0

something like ths

if(1=1)
select * from Table_a
else
slect * from Table_b

without using functions

I am trying something like this

DO $$ 
DECLARE   
  a integer := 10;   
  b integer := 20;  
BEGIN    
  IF a >b THEN     
    select  * from online.fandi_workflow_options ;   
  else 
    select  * from online.credit_workflow_options ;   
 END IF;
END
$$;

Can anyone help me here

6
  • Well that example can be simplified to select * from table_a - what exactly are you trying to do there? What is the real condition you want to test? Commented Jan 29, 2020 at 11:20
  • Please only add one version tag for the real Postgres version you are using. Commented Jan 29, 2020 at 11:21
  • I can achieve this using function .But i dont want to use function.I just want based on condition query should return different result Commented Jan 29, 2020 at 11:24
  • Again: show us the real condition you are trying to apply here. Your example makes no sense. And please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data as formatted text. See here for some tips on how to create nice looking text tables. (edit your question - do not post code or additional information in comments) Commented Jan 29, 2020 at 11:25
  • Edited.But here I am getting some error. Commented Jan 29, 2020 at 11:28

1 Answer 1

2
select  * from online.fandi_workflow_options 
where a > b 
union
select  * from online.credit_workflow_options
where a <= b 

You can usually replace a logical "if" with a "where" clause; in your case, you're selecting from two different tables, so you have to use a union. This query only works if both tables have the same columns - if not, you can select explicit column names, and add "bogus" columns to each select statement to make them identical.

Sign up to request clarification or add additional context in comments.

3 Comments

UNION ALL seems appropiate here. (except for the case of a or b being NULL)
Scenario is to check if the column exists in the table then we have to do a select query
@Madhu, your question is really hard to answer unless you provide specifics. Your example doesn't make much sense - by providing hard coded values for a and b, you're always going to get the same execution path. "If the column exists" will also always give the same execution path unless you're dynamically adding columns to tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.