If exist ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bharadwajrv
    New Member
    • May 2007
    • 26

    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.
  • chandu031
    Recognized Expert New Member
    • Mar 2007
    • 77

    #2
    Originally posted by bharadwajrv
    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!

    Comment

    • frozenmist
      Recognized Expert New Member
      • May 2007
      • 179

      #3
      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:
      [code=sql]

      IF NOT EXISTS (SELECT * FROM Schema.Table1 WHERE ColumnName = 'I1')
      THEN
      SELECT <column> into <variable> FROM Schema.Table1 WHERE ColumnName = 'I2';
      END IF
      [/code]

      Cheers

      Comment

      • bharadwajrv
        New Member
        • May 2007
        • 26

        #4
        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

        Comment

        • frozenmist
          Recognized Expert New Member
          • May 2007
          • 179

          #5
          Hi Venu,
          You can run this inside a block

          [CODE=SQL]
          BEGIN ATOMIC
          IF NOT EXISTS (SELECT * FROM Schema.Table1 WHERE ColumnName = 'I1')

          THEN
          SELECT * FROM Schema.Table1 WHERE ColumnName = 'I2';

          END IF;
          END
          [/CODE]

          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

          Comment

          Working...