Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF NOT EXIST syntax

Status
Not open for further replies.

omacron

Technical User
Feb 5, 2002
149
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.
 
Hi Omacron and welcome to DB2.
The NOT EXISTS clause is part if SQL and is generally used to determine whether you pick up data or not, such as
Code:
SELECT DATA
FROM TABLE_A A
WHERE KEY1 = VALUE
AND NOT EXISTS
   (SELECT DATA
    FROM TABLE_B B
    WHERE B.KEY2 = A.KEY2)

An IF statement is not part of SQL and can be found in a programming language, like an SQL procedural scripting language often used in stored procedures.

I have a feeling that you may be getting the two confused, and trying to combine the two.

Let us know what operating system and platform you are working on, what language you are using to deliver your SQL to DB2, and what you are trying to achieve. I'm fairly certain that the people in this forum will be able to advise you on what to do next.

Hope this helps.

Marc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top