SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert missing rows to table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eirikr_1
Starting Member

26 Posts

Posted - 01/21/2013 :  15:49:23  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 01/21/2013 :  15:59:50  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000