I have an SQL table function, where I wanted to do effectively this:
Based on some earlier logic or parameter, return results from one table or another table.
But it would not let me have two return statements, So in the end I did this:
Union both tables, and condition each on the earlier logic so one of the two will always return nothing.
Is there a better way?
I now know I could have opened the required select in a cursor and piped it to the return (I didn't know about PIPE when I wrote this), but that seems more complicated as I would need to define two cursor loops.
Code:
if a = 1 then return select a,b,c from table1 where e=f; else then return select a,b,c from table2 where e=f; end if;
But it would not let me have two return statements, So in the end I did this:
Code:
return select a,b,c from table1 where e=f and a = 1 union all select a,b,c from table2 where e=f and a <> 1;
Union both tables, and condition each on the earlier logic so one of the two will always return nothing.
Is there a better way?
I now know I could have opened the required select in a cursor and piped it to the return (I didn't know about PIPE when I wrote this), but that seems more complicated as I would need to define two cursor loops.
Comment