By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,419 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,419 IT Pros & Developers. It's quick & easy.

Set based solution instead of Cursor

P: 77
Can anyone provide a simple example of a set based solution to this? Not sure if there is one, but before I put this thing into production I thought I would ask.

Get all records matching condition in Table A
If record does not exist in Table B Insert it
Otherwise Update the existing record in Table B from Table A values

Thanks in advance.
May 21 '09 #1
Share this Question
Share on Google+
3 Replies


ck9663
Expert 2.5K+
P: 2,878
Two queries:

1. UPDATE for all existing records.

2. INSERT INTO for the new ones


--- CK
May 22 '09 #2

P: 77
CK,

I guess the problem I have is that I don't have a unique constraint to restrict the INSERT INTO from duplicating all records. I'd rather not modify that.

Thanks for the answer.
May 26 '09 #3

ck9663
Expert 2.5K+
P: 2,878
Cant' you handle that in WHERE clause?

Something like...

Expand|Select|Wrap|Line Numbers
  1.  
  2. INSERT INTO TargetTable 
  3. SELECT s.* from SourceTable s
  4. where not exists (selec 1 from TargetTable t where t.key = s.key)
  5.  
  6.  
May 26 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.