I have below table and I want to insert values into the employeenew and department table from another table (old system) for new requirement.
Employeenew` (target table)
Create table Employeenew(empo int, empname varchar(50))
Departmentnew
Create table Departmentnew(Dname varchar(50),Location varchar(50))
Currently I am using these tables in the old system:
Create table tables(id int, tableid int, tablename varchar(20))
insert into tables
values (1, 101, 'Employee'), (2, 102, 'Department')
This table contains columns details and table id details:
Create table fields (id int, fieldid int, fieldname varchar(20), fieldtype varchar(100), tableid int)
insert into fields
values (1, 1001, 'empno', 'int', 101),
(2, 1002, 'empname', 'varchar(50)', 101),
(3, 1003, 'dname', 'varchar(50)', 102),
(4, 1004, 'loc', 'varchar(50)', 102);
Below table contains entity (row) details. Each row contains an entityid
Create table entitylistings (id int, entityid int, tableid int)
insert into entitylistings
values (1, 10001, 101), (2, 10002, 101), (3, 10003, 102),(4, 10004, 102)
Below table contains column values for each row.
Create table tablecontents(id int, fieldid int, entityid int, value varchar(max))
insert into tablecontents
values (1, 1001, 10001, 501), (2, 1002, 10001, 'PAUL'),
(3, 1001, 10002, 502), (4, 1002, 10002, 'RAJ'),
(5, 1003, 10003, 'Computer'), (6, 1004, 10003, 'usa')
(7, 1003, 10004, 'Physics'),(8, 1004, 10004, 'India')
Required output
I want to insert the records into Employeenew table(target table) from the table contents table of employee details(empno,ename) and insert into Departmentnew(target table) from the table contents table of department details(dname,location)
Output
Employeenew Table
EMPNO EMPNAME
501 PAUL
502 RAJ
Departmentnew
Dname Location
Computer USA
Physics INDIA