INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

IF NOT EXIST syntax

IF NOT EXIST syntax

(OP)
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.

RE: IF NOT EXIST syntax

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
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close