SQL query for the below scenario
eg: Employee table has 10 rows
The TempEmployee table has 5 rows
TempEmp table should compare with the employee table and insert rows that are not present in the TempEmp
select emp.no, emp.name from dbo.Employee emp
left join dbo.TempEmp temp on temp.no =emp.no
where emp.no is not null and ltrim(rtrim(emp.no))

Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 15, 2023, 9:12 AM EST
To achieve this, you can use the
INSERT INTOstatement along with theSELECTstatement and theLEFT JOINclause. Here's an example SQL query that accomplishes the task:INSERT INTO dbo.TempEmp (no, name) SELECT emp.no, emp.name FROM dbo.Employee emp LEFT JOIN dbo.TempEmp temp ON temp.no = emp.no WHERE emp.no IS NOT NULL AND LTRIM(RTRIM(emp.no)) IS NULL;You can compare the
Employeetable with theTempEmptable and insert the rows that are not present in theTempEmptable.Jignesh KumarRank #91 · 22.9K reputationPosted Dec 17, 2023, 11:41 PM EST
Hello Meghna,
You can try this one,
INSERT INTO dbo.TempEmp (no, name, address, gender) SELECT emp.no, emp.name, emp.address, emp.gender FROM dbo.Employee emp LEFT JOIN dbo.TempEmp temp ON temp.no = emp.no WHERE emp.no IS NOT NULL AND temp.no IS NULL;