If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > "Insert Into... Where..." with multiple criteria

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-11, 04:42
tinyevil tinyevil is offline
Registered User
 
Join Date: Oct 2011
Posts: 11
"Insert Into... Where..." with multiple criteria

Good morning all.

I'm sure this is pretty simple, but i'm failing at the moment.

I want to run an insert sql script to insert new data into a table.

Here is a copy of my script so far.

Code:
insert into TargetTbl (Prefix,Bet1,Updated,Key1,Key2,Key3,Bet2)
select Prefix,Key3,GETDATE(),Key1,Key2,Key3,Key3
from SourceTbl
where ...
This is where i get stuck... I only want records to be imported where the Prefix in SourceTbl is "A", and the fields "Key1" and "Key2" are not collectively already in TargetTbl.

I should probably explain that i already have an UPDATE statement to cater for records where "Key1" and "Key2" are collectively in TargetTbl. I wanted to use a MERGE statement, but due to the unchangeable compatibility level of our database, its not going to happen.

If someone could help or point me in the right direction, i'd greatly appreciate it!

Thank you all.
Reply With Quote
  #2 (permalink)  
Old 10-25-11, 07:25
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
Practice doing a simple SELECT to verify you get the wanted rows:

WHERE SourceTbl = 'A'
AND NOT EXISTS
(SELECT * FROM TargetTbl as TT WHERE tt.key1 = st.key1 AND tt.key2 = st.key2)
Reply With Quote
  #3 (permalink)  
Old 10-26-11, 16:20
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You may also want to have a look at the MERGE statement.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 10-27-11, 12:20
tinyevil tinyevil is offline
Registered User
 
Join Date: Oct 2011
Posts: 11
Stolze, i cannot use the MERGE statement as the compatibility level of the database is too low, as previously stated.

JarlH, thanks for your help. I was only missing the 'EXISTS' or 'NOT EXISTS' section off my script, all working now, thank you.
Reply With Quote
  #5 (permalink)  
Old 11-07-11, 19:52
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Sorry, I didn't know what you mean by "compatibility level".
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Tags
insert, multiple criteria, sql

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On