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 > IF NOT EXIST syntax

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-22-09, 11:51
xaphann xaphann is offline
Registered User
 
Join Date: Jul 2009
Posts: 3
IF NOT EXIST syntax

Hi

I am new to DB2 and have a simple problem. Trying to get an IF NOT EXIST statement work. Here is my code;

Code:
IF NOT EXISTS (select * from ABC.table1 where field1 = 1100200)
	THEN
		INSERT INTO ABC.table1 ("field1") VALUES (1100200);
END IF;
Toad keeps telling me there is an error at the first IF. My googling tells if not exist does work I just can't figure out what I am doing wrong.
Reply With Quote
  #2 (permalink)  
Old 07-22-09, 12:05
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
IF cannot be used standalone in that context. Try wrapping your code in BEGIN ATOMIC ... END. Make sure you use the outer statement delimiter other than ";".
Reply With Quote
  #3 (permalink)  
Old 07-22-09, 12:11
xaphann xaphann is offline
Registered User
 
Join Date: Jul 2009
Posts: 3
hehe odd, I just tried;
Code:
BEGIN ATOMIC
IF NOT EXISTS (select * from ABC.table1 where field1 = 1100200)
	THEN
		INSERT INTO ABC.table1 ("field1") VALUES (1100200);
END IF;
END;
Although that goes on for what seems forever. Doing this;
Code:
BEGIN ATOMIC
IF NOT EXISTS (select * from ABC.table1 where field1 = 1100200)
	THEN
		INSERT INTO ABC.table1 ("field1") VALUES (1100200);
END IF;
END@
Returns the result of;
Code:
DB2 Database Error: ERROR [42601] [IBM][DB2/NT] SQL0104N  An unexpected token "END-OF-STATEMENT" was found following "2,0);  END IF;  END@".  Expected tokens may include:  "JOIN <joined_table>".  LINE NUMBER=7.  SQLSTATE=42601
So still not sure what I am doing wrong.
Reply With Quote
  #4 (permalink)  
Old 07-22-09, 16:07
DBFinder DBFinder is offline
Registered User
 
Join Date: Sep 2008
Location: Toronto,Canada
Posts: 606
did you this use syntax to run this code

>db2 -td@ -f mysql.db2

You need to tell DB2 about the terminator you used !!
Reply With Quote
  #5 (permalink)  
Old 07-22-09, 16:40
xaphann xaphann is offline
Registered User
 
Join Date: Jul 2009
Posts: 3
ahhh that worked, but not quite the solution I was looking for. Is there a way to set the terminator in the actual script?

Problem is that this script will be past to multiple people that will execute differently. Some will use Toad, some will use the Command line editor. Having them manually do this will just cause me more problem (your script doesn't work etc...).

Basically all I want is for the user load the script hit the execute button and it works.
Reply With Quote
  #6 (permalink)  
Old 07-22-09, 16:42
DBFinder DBFinder is offline
Registered User
 
Join Date: Sep 2008
Location: Toronto,Canada
Posts: 606
--#set Terminator $

since this line on the terminator will be $
Reply With Quote
  #7 (permalink)  
Old 07-22-09, 16:45
DBFinder DBFinder is offline
Registered User
 
Join Date: Sep 2008
Location: Toronto,Canada
Posts: 606
--#SET TERMINATOR $
This will ser the terminator to $
To change the terminator in the CLP on the fly, the following syntax will set it back:
--#SET TERMINATOR
Reply With Quote
  #8 (permalink)  
Old 07-22-09, 17:58
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
For me the logic "IF --- END IF" is extra. You have to use:

Quote:
INSERT INTO ABC.table1 ("field1")
select 1100200
from sysibm.sysdummy1
where
NOT EXISTS (select * from ABC.table1 where field1 = 1100200)
Lenny K.
Reply With Quote
  #9 (permalink)  
Old 07-22-09, 18:03
DBFinder DBFinder is offline
Registered User
 
Join Date: Sep 2008
Location: Toronto,Canada
Posts: 606
good idea

thanks
Reply With Quote
  #10 (permalink)  
Old 07-23-09, 09:49
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
No problem !
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