469,265 Members | 2,020 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,265 developers. It's quick & easy.

If exist ?

Any idea about the DB2 equivalent statement for ‘If Exists’ ??

i'm trying with

[PHP]

IF NOT EXISTS (SELECT * FROM Schema.Table1 WHERE ColumnName = 'I1')
THEN
SELECT * FROM Schema.Table1 WHERE ColumnName = 'I2'
END IF
[/PHP]

i'm gettin error message:-
ERROR [42601] [IBM][DB2/NT] SQL0104N An unexpected token "EXISTS" was found following "IF NOT ". Expected tokens may include: "JOIN". SQLSTATE=42601
please can you help me...

Thanks.
May 30 '07 #1
4 30203
chandu031
78 Expert
Any idea about the DB2 equivalent statement for ‘If Exists’ ??

i'm trying with

[PHP]

IF NOT EXISTS (SELECT * FROM Schema.Table1 WHERE ColumnName = 'I1')
THEN
SELECT * FROM Schema.Table1 WHERE ColumnName = 'I2'
END IF
[/PHP]

i'm gettin error message:-

please can you help me...

Thanks.
Hi bharadwajrv,

IF NOT exists does EXIST in DB2 and it has the same syntax as what you have mentioned. Your problem might be that of missing semicolons.
Check it out and let me know.

Cheers!
May 31 '07 #2
frozenmist
179 Expert 100+
Hi,
I believe this is part of a stored procedure.
If so why are you doing a
Select * from table where condition ,
In the if block.
Where are you selecting the values into.
Instead of that give like:
Expand|Select|Wrap|Line Numbers
  1.  
  2.       IF NOT EXISTS (SELECT * FROM Schema.Table1 WHERE ColumnName = 'I1')
  3.       THEN
  4.           SELECT <column> into <variable> FROM Schema.Table1 WHERE ColumnName = 'I2';
  5.       END IF
  6.  
Cheers
May 31 '07 #3
Thanks for your reply.

chandu031 - I tried with ";" at end of the statment, but no joy. If you have any example, please can you post them here. Thanks.

frozenmist - Currently this is not a stored procedure... This is a simple inline sql statement in the application...

My requirement is :- need to check if a record exist with value "I1", in that return the record with value 'I1' , if that does not exist, return record with 'i2'..

Any idea on this... Can this be run as a block of SQL statement instead of Stored Proc.

cheers
Venu
May 31 '07 #4
frozenmist
179 Expert 100+
Hi Venu,
You can run this inside a block

Expand|Select|Wrap|Line Numbers
  1.   BEGIN ATOMIC
  2.       IF NOT EXISTS (SELECT * FROM Schema.Table1 WHERE ColumnName = 'I1')
  3.  
  4.       THEN
  5.             SELECT * FROM Schema.Table1 WHERE ColumnName = 'I2';
  6.  
  7.       END IF;
  8.   END
  9.  
You cant see the result of the select query if you are running it in SQL prompt.
It compiles like a procedure only. You have to get the result set from it then.

Cheers
Jun 1 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by zhisol | last post: by
4 posts views Thread by Mike | last post: by
52 posts views Thread by paytam | last post: by
1 post views Thread by sword | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.