Skip to main content

Need IF ELSE functionality workaround


Navin_C
Forum|alt.badge.img+2
Is there any way of using a IF ELSE statement in Vertica. Any workaround on this. I have a scenario where I need to count the (select query value) store it in a variable and if the variable value is greater then 1 I need to execute a bunch of SQL statements. Any help appreciated.

5 replies

Forum|alt.badge.img+2
  • Participating Frequently
  • May 30, 2013
Hi, I believe that you are looking for the CASE expression. https://my.vertica.com/docs/6.1.x/HTML/index.htm#3056.htm Please let me know if this helps. Eugenia

Navin_C
Forum|alt.badge.img+2
  • Author
  • Participating Frequently
  • May 30, 2013
Hello Eugenia, I already know the case expression, but I don't think this can be handeld by the case expression as my scenario need to execute a bunch of sql statement depending on the count of a select query if it is greater then or equal to 1.

eli_revach
Forum|alt.badge.img+2
  • Participating Frequently
  • May 30, 2013
See this working example : select case when memory_inuse_kb =0 then (select 1 from dual) when memory_inuse_kb >0 then (select 2 from dual) end from resource_pool_status

Forum|alt.badge.img+2
  • Participating Frequently
  • May 30, 2013
It might help to collect the 'bunch of sql' into an sql function: https://my.vertica.com/docs/6.1.x/HTML/index.htm#15022.htm Then execute the function contingent on your predicate: select my_bunch_of_sql() where (select count(*) from t where something) >= 1; or, simpler and probably faster: select my_bunch_of_sql() where exists (select * from t where something);

Navin_C
Forum|alt.badge.img+2
  • Author
  • Participating Frequently
  • May 31, 2013
Hello Greorge, Can we use the SQL statments like insert, update and delete in the return clause of the Functions. I don't think the functions are so flexible to make it that way. Can you please suggest.