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 > Database Server Software > DB2 > How to test values before insert

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-20-09, 09:36
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Question How to test values before insert

Hi,
does anybody know how to test in script files if values already exists in a table(s), please?

I have following data.sql script:
---
CONNECT TO MyDB;
INSERT INTO APPCONFIG (KEY, VALUE) VALUES ('someKey', 'someValue');
CONNECT RESET;
---
(APPCONFIG is my custom table)

I run this script using DB2 command line tool:
---
DB2 -tf data.sql
---

My problem is, that this script may be run more than once. So i need to add some IF statement to test if the key alerady exists. I've tried a lot of IF SELECT COUNT(*) FROM ... statements, but I still receive syntax errors etc.

Can anybody help me, please?
Reply With Quote
  #2 (permalink)  
Old 04-20-09, 10:02
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What DB2 version and OS?

Andy
Reply With Quote
  #3 (permalink)  
Old 04-20-09, 10:09
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Sorry, sure - DB2 v9.5.0.808 (Express), OS Win 2003 Server
(I thought the language is version and OS independent).
Reply With Quote
  #4 (permalink)  
Old 04-20-09, 10:30
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I wanted the version info so I know what features are available to you.

Why do you want to test the existence first? Why not just let it fail?

Andy
Reply With Quote
  #5 (permalink)  
Old 04-20-09, 10:37
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Thank you for your reply.

The KEY column is not a PK, there is also ID (generated as identity) and the code will not fail, but insert new row into the table. Sure, in this scenario the KEY could be a PK, but I have also tables as USERS, ROLES, EMPLOYEES etc that I also need to pre-fill by the same script.
Reply With Quote
  #6 (permalink)  
Old 04-20-09, 10:41
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Take a look at the MERGE statement to use instead of the INSERT statement.

Andy
Reply With Quote
  #7 (permalink)  
Old 04-20-09, 10:51
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
I alraedy did, but it seems to me the MERGE statement meges data from another /source/ table, not from defined values, doesn't?

Thanks a lot!

Frantisek.
Reply With Quote
  #8 (permalink)  
Old 04-20-09, 10:56
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You can use MERGE from defined values, something like:


MERGE into MyTable as m using (values (cast('somekey' as varchar(12)) as KEY, cast('somevalue' as varchar(20)) as VALUE) ) as x on (m.key = x.key)
WHEN NOT MATCHED THEN insert...


Andy
Reply With Quote
  #9 (permalink)  
Old 04-20-09, 11:28
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
if statements are not allowed in db2 scripts
on unix use shell scripting to do or on windows any other supported language todo so. there is something like dos scripting.. or perl..
or use a stored procedure
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #10 (permalink)  
Old 04-20-09, 11:39
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Andy's code must be syntax error of naming columns in values clause.
Correction would like:

MERGE into MyTable as m using (values (cast('somekey' as varchar(12)), cast('somevalue' as varchar(20))) ) as x(key, value) on (m.key = x.key)
WHEN NOT MATCHED THEN insert...
Reply With Quote
  #11 (permalink)  
Old 04-20-09, 11:58
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
If you have a unique constraint on the KEY column, just ignore the error. If you have not a unique constraint, create it.
Reply With Quote
  #12 (permalink)  
Old 04-21-09, 02:30
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
Quote:
Originally Posted by przytula_guy
if statements are not allowed in db2 scripts
I think you can you use the Begin atomic compound stmts in scripts
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #13 (permalink)  
Old 04-21-09, 03:18
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
That's it! Thank you both.
Reply With Quote
  #14 (permalink)  
Old 04-21-09, 04:09
frun frun is offline
Registered User
 
Join Date: Apr 2009
Posts: 21
Quote:
Originally Posted by rahul_s80
I think you can you use the Begin atomic compound stmts in scripts
I've solved my primary problem by MERGE statement, but I'm newbie in DB2 world, can you please explain what do you mean by BEGIN ATOMIC statement?

Thanks a lot!
Reply With Quote
  #15 (permalink)  
Old 04-21-09, 05:30
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
You can conditional stmts in a begin atomic script as :

/TEMP 21$ cat 1.txt
Begin Atomic

if ( select count(*) from dim.test4 ) = 0
then
insert into dim.test4 values (4);
else
update dim.test4 set no=no+10 ;
end if;

end @

/TEMP 24$ db2 -td@ -f 1.txt
DB20000I The SQL command completed successfully.
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
Reply

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