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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 check for primary key exist in table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

abul_mohsin
Starting Member

Saudi Arabia
20 Posts

Posted - 02/13/2013 :  10:12:05  Show Profile  Reply with Quote
Hi,
I have two tables TEMPTable and ArchiveTable i am using below statement
to insert the rows from TEMPTable to ArchiveTable. Before inserting i need to check, if primary key row of TEMPTable already exist DONOT insert into ArchiveTable

INSERT INTO ArchiveTable
SELECT FROM TEMPTable


Thanks & Best Regard's
Abul Mohsin

James K
Flowing Fount of Yak Knowledge

3334 Posts

Posted - 02/13/2013 :  10:30:07  Show Profile  Reply with Quote
One way to do it is as follows (i am showing col1, col2 just for illustration. You might have more or less number of columns to insert):
INSERT INTO ARchiveTable (pk, col1, col2)
SELECT t.pk, t.col1, t.col2 FROM
TEMPtable t 
WHERE NOT EXISTS (SELECT * FROM ArchiveTable a WHERE a.pk = t.pk)
Go to Top of Page

abul_mohsin
Starting Member

Saudi Arabia
20 Posts

Posted - 02/13/2013 :  10:34:39  Show Profile  Reply with Quote
Thank you ....I appreciate your help

Thanks & Best Regard's
Abul Mohsin
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.05 seconds. Powered By: Snitz Forums 2000