Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert missing rows to table

Author  Topic 

eirikr_1
Starting Member

27 Posts

Posted - 2013-01-21 : 15:49:23
i am developing an web app allowing user to upload data from an .xls file to a "Temp" table. I want to insert only rows in Temp that are not in "MasterList" table

Data in Temp
ComputerName AuditID IAV Date
AAA Au1 I1 1/1/13
BBB Au2 I2 1/1/13
CCC Au3 I3 1/1/13
AAA Au4 I4 2/1/13

Data in Master List
AAA Au1 I1 1/1/13

Please help me with a TSQL to insert BBB, CCC, DDD from Temp to MasteList only.
Thank you.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-21 : 15:59:50
Couple of different ways to do it - take your pick
INSERT INTO MasterList
(ComputerName, AuditId, IAV, Date)
SELECT
t.ComputerName,
t.AuditID,
t.IAV,
t.Date
FROM
Temp t
LEFT JOIN MasterList m
ON m.ComputerName = t.ComputerName
WHERE
m.ComputerName IS NULL;


INSERT INTO MasterList
(ComputerName, AuditId, IAV, Date)
SELECT
t.ComputerName,
t.AuditID,
t.IAV,
t.Date
FROM
Temp t
WHERE NOT EXISTS
( SELECT * FROM MasterList m
WHERE m.ComputerName = t.ComputerName);
Go to Top of Page
   

- Advertisement -