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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 check for primary key exist in table

Author  Topic 

abul_mohsin
Starting Member

21 Posts

Posted - 2013-02-13 : 10:12:05
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-13 : 10:30:07
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

21 Posts

Posted - 2013-02-13 : 10:34:39
Thank you ....I appreciate your help

Thanks & Best Regard's
Abul Mohsin
Go to Top of Page
   

- Advertisement -