IF EXISTS SYNTAX
Posted in 1997
Topics: General Discussion
Hi Gurus, I'm having a problem with the if exists condition. I'm trying to do a selection into a variable if the record exists in the table. Example: Let's assume a table named 'test' with columns col_a and col_b My CASE I procedure body is: CASE I: IF exists (select * from test where col_a = '1') THEN state = 'success'; else state = 'failure'; end if; return state; end procedure; In this scenario, the query works fine and does what I want to do. (retuns success if cola = 1 exists) My problem is, I want to select the values into variables if the condtion is true. CASE II: DEFINE a_col_b char(); IF EXISTS (select col_b into a_col_b from test where col_a ='1') THEN ..... END PROCEDURE; Informix does not like this and returns a syntax error. The workaround that I have now is CASE III: if exists(select * from test where col_a = '1') then select col_b into col_b where col_a = '1'; else ... end procedure; I prefer using CASE II since we don't have to select * first and then select into variables if the condition is true. Is there a better way of doing this. Is there a reason behind this scenario why informix has not impelented this style. Please send your comments. Thanks, Sathya