Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi,I have two tables TEMPTable and ArchiveTable i am using below statementto insert the rows from TEMPTable to ArchiveTable. Before inserting i need to check, if primary key row of TEMPTable already exist DONOT insert into ArchiveTableINSERT INTO ArchiveTableSELECT FROM TEMPTableThanks & Best Regard'sAbul 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 FROMTEMPtable t WHERE NOT EXISTS (SELECT * FROM ArchiveTable a WHERE a.pk = t.pk)
abul_mohsin
Starting Member
21 Posts
Posted - 2013-02-13 : 10:34:39
Thank you ....I appreciate your helpThanks & Best Regard'sAbul Mohsin