I have the 5 employee tables as shown below.
---Table: Emp_1
CREATE TABLE Emp_1
(
Emp_ID VARCHAR(10),
Emp_FName VARCHAR(10),
Emp_LName VARCHAR(10),
Emp_PNumber VARCHAR(10)
);
---Table: Emp_2
CREATE TABLE Emp_2
(
Emp_ID VARCHAR(10),
Emp_FName VARCHAR(10),
Emp_LName VARCHAR(10),
Emp_PNumber VARCHAR(10)
);
---Table: Emp_3
CREATE TABLE Emp_3
(
Emp_ID VARCHAR(10),
Emp_FName VARCHAR(10),
Emp_LName VARCHAR(10),
Emp_PNumber VARCHAR(10)
);
---Table: Emp_4
CREATE TABLE Emp_4
(
Emp_ID VARCHAR(10),
Emp_FName VARCHAR(10),
Emp_LName VARCHAR(10),
Emp_PNumber VARCHAR(10)
);
---Table: Emp_5
CREATE TABLE Emp_5
(
Emp_ID VARCHAR(10),
Emp_FName VARCHAR(10),
Emp_LName VARCHAR(10),
Emp_PNumber VARCHAR(10)
);
--Insertion: Emp_1
INSERT INTO Emp_1 VALUES('A1','Abram','Mak','123');
INSERT INTO Emp_1 VALUES('A2','Sam','William','321');
--Insertion: Emp_2
INSERT INTO Emp_2 VALUES('A3','John','Marsh','456');
INSERT INTO Emp_2 VALUES('A4','Tom','Lee','654');
--Insertion: Emp_3
INSERT INTO Emp_3 VALUES('A5','Abram','Mak','789');
INSERT INTO Emp_3 VALUES('A6','Shawn','Meben','987');
--Insertion: Emp_4
INSERT INTO Emp_4 VALUES('A7','Sam','William','189');
INSERT INTO Emp_4 VALUES('A8','Mark','Boucher','287');
--Insertion: Emp_5
INSERT INTO Emp_5 VALUES('A9','Gery','Joy','907');
INSERT INTO Emp_5 VALUES('A10','Anthony','Desosa','977');
Now I will insert the each table name into Container table.
I have the following table called as Container
which contains the table names, which may be many in my
case I have just inserted 5 as shown below.
--Table : Container
CREATE TABLE Container
(
TableName VARCHAR(50)
);
--Insertion
INSERT INTO Container VALUES('Emp_1');
INSERT INTO Container VALUES('Emp_2');
INSERT INTO Container VALUES('Emp_3');
INSERT INTO Container VALUES('Emp_4');
INSERT INTO Container VALUES('Emp_5');
Note: Now I want to delete the duplicate row from each table and want to keep the original as it is. And the condition for delete the duplicate row is:
If the Emp_FName
and Emp_LName
is match with the other tables then the duplicated row has to be deleted and
original row keep as it is.
In my example the Emp_FName
and Emp_LName
: 'Abram','Mak'
repeated in the table Emp_3
which has to be deleted and original
one which is present in the table Emp_1
has keep as it is.
And Emp_FName
and Emp_LName
: 'Sam','William'
repeated in the table Emp_4
which has to be deleted and original
one which is present in the table Emp_1
has keep as it is.
For single Table: For single table I can use the following script to delete the duplicate one and keep the original one.
;WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY Emp_FName,Emp_LName ORDER BY Emp_FName) Row_Number FROM Emp_1
)
DELETE FROM CTE
WHERE Row_Number > 1;
My question is how to delete duplicate row from the multiple tables which are dynamic I mean in my Container
table the tables
may be more than 5 also.