ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQL PL Table function that returns from prepared SQL?

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

  • SQL PL Table function that returns from prepared SQL?

    I would like to create an SQL table function, written in SQL_PL that does the following:

    Receive a code as a parameter
    Look that code up in a table that contains an SQL select statement string
    Execute that SQL and return the results as the table returned

    Is that possible in SQL PL? If it is I can't work out how.

    If it isn't then I know I can write an External SQL Table Function and implement it in SQLRPGLE.

  • #2
    As long as the SQL Statements always return the same columns (Number of Columns with the same data type and length in the same sequence) it should be possible to create a piped UDTF.
    Declare Cursor For DynSELECT
    Prepare DynSELECT for YourSELECT Statement
    OPEN Cursor
    Loop through the cursor
    Return each row with PIPE
    Close the Cursor
    Return

    If the select statements do not always return the same columns in the same sequence with the same data type ... it is not possible (because you have to define the output table!)

    Comment


    • Herb...
      Herb... commented
      Editing a comment
      TIL about the PIPE command. Thank you, Birgitta!!

  • #3
    They do return the same columns

    I didn't know about PIPE - would that be this: https://www.ibm.com/docs/en/i/7.3?to...pipe-statement

    Comment


    • Herb...
      Herb... commented
      Editing a comment
      That's the very one!! I tried an example based on the example from your link, and it worked well!

      PIPE(column1, column2, column3) ;

      in the order that your columns are specified in the RETURNS TABLE definition

  • #4
    It worked, thanks!

    Comment

    Working...
    X