ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQL table function that returns one of two different select statements?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQL table function that returns one of two different select statements?

    I have an SQL table function, where I wanted to do effectively this:

    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;
    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:
    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.

  • #2
    Define 2 Cursors - Depending on the condition read the first or the second cursor and return the rows with the PIPE Statement

    Comment


    • #3
      Awww I hoped there was a neater way

      Thanks as always Birgitta!

      Comment

      Working...
      X