I have table which stores details of each entity(employee, department). I want to build dynamic query from that table.
CREATE TABLE MyTable
(
Id int primary key,
EntityId int,
ColumnName varchar(100),
tablename varchar(100)
);
INSERT INTO MyTable (Id, EntityId, ColumnName, tableName)
VALUES (1,1,'name','employee');
INSERT INTO MyTable (Id, EntityId, ColumnName, tableName)
VALUES (2,1,'id','employee');
INSERT INTO MyTable (Id, EntityId, ColumnName, tableName)
VALUES (3,1,'salary','employee');
INSERT INTO MyTable (Id, EntityId, ColumnName, tableName)
VALUES (4,2,'name','departement');
INSERT INTO MyTable (Id, EntityId, ColumnName, tableName)
VALUES (5,2,'location','departement');
INSERT INTO MyTable (Id, EntityId, ColumnName, tableName)
VALUES (6,2,'id','departement');
Above is my table and insert scripts, How can I write a query which gives me output something like below.
SELECT id,name,salary from employee
SELECT id,location,name from departement
If i have multiple entity I should multiple select statements.