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 2000 Forums
 SQL Server Development (2000)
 Inserting ONLY new records from a temp table to a live table

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-12-13 : 08:23:13
Wayne writes "Currently I use a cursor to loop thru all the records in the temp table, I only insert record if it does not already exist in the live table. Is there a more efficient way of doing this? (like without using the cursor)"

nr
SQLTeam MVY

12543 Posts

Posted - 2004-12-13 : 08:36:43
insert tbl
select tmp.*
from #temp tmp
left join tbl
on tmp.pk = tbl.pk
where tbl.pk is null

You might need a distinct too.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dsdeming

479 Posts

Posted - 2004-12-13 : 08:38:07
Try checking for nulls from an outer join, something like this:

insert into tablea( col1, col2... )
select b.col1, b.col2...
from tableb b
left join tablea a on b.key = a.key
where a.key is null

Dennis
Go to Top of Page
   

- Advertisement -