Skip to main content
JR Moore
Participating Frequently
Dec 17, 2021 11:41 AM UTC

SQL ODBC TO UNIVERSE LIMIT ROWS

Can someone tell me the proper syntax for SQL statement to Universe. I have a large file that I need to fetch 10000 rows at a time from Universe that will update a remote AZURE DB daily. I don't have control over the AZURE site as EDA seems like a much better solution to keep the remote data current.

I have tried:

SELECT TOP 10000 * FROM FILE;
SELECT * FROM FILE LIMIT 10000;
SELECT * FROM FILE FETCH FIRST 10000;
SELECT * FROM FILE WHERE ROWNUM < 10000;

Thanks JR Moore


------------------------------
JR Moore
IT Special Projects
Rocket Forum Shared Account
Hillsboro US
------------------------------

    21 replies

    Gregor Scott
    Participating Frequently
    Dec 19, 2021 7:19 PM UTC
    Can someone tell me the proper syntax for SQL statement to Universe. I have a large file that I need to fetch 10000 rows at a time from Universe that will update a remote AZURE DB daily. I don't have control over the AZURE site as EDA seems like a much better solution to keep the remote data current.

    I have tried:

    SELECT TOP 10000 * FROM FILE;
    SELECT * FROM FILE LIMIT 10000;
    SELECT * FROM FILE FETCH FIRST 10000;
    SELECT * FROM FILE WHERE ROWNUM < 10000;

    Thanks JR Moore


    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------
    I believe the keyword you are looking for is SAMPLE {n}
    This will select the First {n} rows of the database file.
    e.g
    SELECT * FROM FILE SAMPLE 10000;



    There is also the SAMPLED {n} keyword, which select every {n}th row of the database, but that is not what you seem to be wanting.



    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions Pty Ltd
    Mount Waverley VIC AU
    ------------------------------
    JR Moore
    JR MooreAuthor
    Participating Frequently
    Dec 20, 2021 9:49 AM UTC
    I believe the keyword you are looking for is SAMPLE {n}
    This will select the First {n} rows of the database file.
    e.g
    SELECT * FROM FILE SAMPLE 10000;



    There is also the SAMPLED {n} keyword, which select every {n}th row of the database, but that is not what you seem to be wanting.



    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions Pty Ltd
    Mount Waverley VIC AU
    ------------------------------
    Gregor:

    Thanks for the reply to my question. I should have mentioned I tried SAMPLE and it works at TCL but not when calling from an outside program. It also works from the U2ODBC tester in Native SQL but not ODBC Compliant SQL. Does anybody know if there is a setting to allow Native SQL from outside Universe?

    Thanks JR Moore

    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------
    Gregor Scott
    Participating Frequently
    Dec 20, 2021 6:30 PM UTC
    Gregor:

    Thanks for the reply to my question. I should have mentioned I tried SAMPLE and it works at TCL but not when calling from an outside program. It also works from the U2ODBC tester in Native SQL but not ODBC Compliant SQL. Does anybody know if there is a setting to allow Native SQL from outside Universe?

    Thanks JR Moore

    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------

    Thanks for the clarification @JR Moore.

    So you are now looking at the "Programmatic ODBC" stuff.

    Have you tried the SQL "CALL {xx}" syntax?

    You can create a program, or a subroutine if you wish to pass runtime parameters, that can execute an SQL statement, and return an SQL result set back to the caller.
    In the program you can use whatever logic you need to establish a select list, saving it to one of the 1-9 numbers, and then execute your SQL statement that uses the "SLIST" keyword to pick up the select list.


    Here is a snippet from one of my experiments working with the CALL syntax in SQL on UV.
    You can see that I have previously populated select list 9, and the SQL command is using that as the ID filter.
    The rest of the SQL statement is simply column definitions and sort sequencing.

    cmd.SQL := \\FROM INVOICE SLIST 9 \\
    cmd.SQL := \\WHERE INV_GPPctg < \\:pctg.LIMIT

    * Run the SQL command
    if @TTY[1,2] = "uv" then
       * UCI connections have @TTY='uvsrv'
       * Build the record set for the UCI connection
       ST = SQLExecDirect(@HSTMT, cmd.SQL)
    end else
       * Run this interactively at TCL
       perform cmd.SQL:";"
    end




    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions Pty Ltd
    Mount Waverley VIC AU
    ------------------------------
    Kathleen Hambrick
    New Participant
    Dec 21, 2021 9:32 AM UTC

    Thanks for the clarification @JR Moore.

    So you are now looking at the "Programmatic ODBC" stuff.

    Have you tried the SQL "CALL {xx}" syntax?

    You can create a program, or a subroutine if you wish to pass runtime parameters, that can execute an SQL statement, and return an SQL result set back to the caller.
    In the program you can use whatever logic you need to establish a select list, saving it to one of the 1-9 numbers, and then execute your SQL statement that uses the "SLIST" keyword to pick up the select list.


    Here is a snippet from one of my experiments working with the CALL syntax in SQL on UV.
    You can see that I have previously populated select list 9, and the SQL command is using that as the ID filter.
    The rest of the SQL statement is simply column definitions and sort sequencing.

    cmd.SQL := \\FROM INVOICE SLIST 9 \\
    cmd.SQL := \\WHERE INV_GPPctg < \\:pctg.LIMIT

    * Run the SQL command
    if @TTY[1,2] = "uv" then
       * UCI connections have @TTY='uvsrv'
       * Build the record set for the UCI connection
       ST = SQLExecDirect(@HSTMT, cmd.SQL)
    end else
       * Run this interactively at TCL
       perform cmd.SQL:";"
    end




    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions Pty Ltd
    Mount Waverley VIC AU
    ------------------------------
    I'd like to dabble in some sql calls like the one above but I am in Unidata, not universe. Is there a tutorial, class, or well written manual on how to start to use the sql features in unidata? Does the above code work on unidata as well?

    Thanks!

    ------------------------------
    Kathleen Hambrick
    PROGRAMMER
    William C Earhart Co Inc
    Portland OR US
    ------------------------------
    Bill Brutzman
    Participating Frequently
    Dec 21, 2021 11:37 AM UTC
    I'd like to dabble in some sql calls like the one above but I am in Unidata, not universe. Is there a tutorial, class, or well written manual on how to start to use the sql features in unidata? Does the above code work on unidata as well?

    Thanks!

    ------------------------------
    Kathleen Hambrick
    PROGRAMMER
    William C Earhart Co Inc
    Portland OR US
    ------------------------------
    1. Rocket has _ outstandingly good _ documentation (manuals) on using SQL with U2.
    2. The Rocket BDT (Basic Developer ToolKit) is also _ key.

    ------------------------------
    Bill Brutzman
    IT Manager
    Hk Metalcraft Manufacturing Corporation
    Lodi NJ US
    ------------------------------
    Gregor Scott
    Participating Frequently
    Dec 21, 2021 8:00 PM UTC
    I'd like to dabble in some sql calls like the one above but I am in Unidata, not universe. Is there a tutorial, class, or well written manual on how to start to use the sql features in unidata? Does the above code work on unidata as well?

    Thanks!

    ------------------------------
    Kathleen Hambrick
    PROGRAMMER
    William C Earhart Co Inc
    Portland OR US
    ------------------------------
    Here is a fully working example BASIC program (UniVerse, PICK flavour account syntax) for use in SQL calls:

    program TEST.SQLCALL

    * Get LIMIT option
    get(arg.) OPT.VERB else OPT.VERB = ''
    b.LIMIT = (upcase(OPT.VERB) = "LIMIT")
    if b.LIMIT then get(arg.) qty.VERB else qty.VERB = 0

    * Be verbose if we are an interactive session
    b.VERBOSE = (@TTY[1,2] # "uv")

    SEL.CMD = 'SELECT VOC TO 9 WITH F1 = "F]" AND WITH F2 # "/]" AND WITH F2 # "..]" AND WITH F2 # "./]"'
    if b.LIMIT and len(qty.VERB) then SEL.CMD := " SAMPLE ":qty.VERB

    clearselect 9

    if b.VERBOSE then
    crt "Selecting VOC local F items"
    crt "CMD = ":SEL.CMD
    execute SEL.CMD
    end else
    execute SEL.CMD capturing exop
    end

    b.SL9.ACTIVE = selectinfo(9,1)
    qty.SEL = 0
    if b.SL9.ACTIVE then qty.SEL = selectinfo(9,3)

    if b.VERBOSE then
    crt " Select list 9 Active? ":b.SL9.ACTIVE
    crt " Qty Selected = ":qty.SEL
    end

    if b.SL9.ACTIVE then
    SQL.CMD = 'SELECT @ID FMT "25L" AS "VOCNAME", '
    SQL.CMD := 'EVAL "@RECORD<2>" FMT "25L" AS "DATAFILE", '
    SQL.CMD := 'EVAL "@RECORD<3>" FMT "25L" AS "DICTFILE" '
    SQL.CMD := 'FROM VOC SLIST 9 '
    SQL.CMD := 'ORDER BY VOCNAME ASC'

    if @TTY[1,2] = "uv" then
    * UCI connections have @TTY='uvsrv'
    * Build the record set for the UCI connection
    ST = SQLExecDirect(@HSTMT, SQL.CMD)
    end else
    * Run this interactively
    perform SQL.CMD:";"
    end

    end

    @SYSTEM.RETURN.CODE = qty.SEL

    This can be run at TCL like this:
    TEST.SQLCALL LIMIT 200
    or
    TEST.SQLCALL LIMIT 1000

    It can be used in an SQL call with the following SQL statement:
    CALL TEST.SQLCALL LIMIT 200
    or 
    CALL TEST.SQLCALL LIMIT 200

    I think the BASIC program's syntax is something you will need to fine tune for UniData.
    The SQL syntax should be usable as is.

    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions Pty Ltd
    Mount Waverley VIC AU
    ------------------------------
    JR Moore
    JR MooreAuthor
    Participating Frequently
    Dec 22, 2021 11:53 AM UTC
    Here is a fully working example BASIC program (UniVerse, PICK flavour account syntax) for use in SQL calls:

    program TEST.SQLCALL

    * Get LIMIT option
    get(arg.) OPT.VERB else OPT.VERB = ''
    b.LIMIT = (upcase(OPT.VERB) = "LIMIT")
    if b.LIMIT then get(arg.) qty.VERB else qty.VERB = 0

    * Be verbose if we are an interactive session
    b.VERBOSE = (@TTY[1,2] # "uv")

    SEL.CMD = 'SELECT VOC TO 9 WITH F1 = "F]" AND WITH F2 # "/]" AND WITH F2 # "..]" AND WITH F2 # "./]"'
    if b.LIMIT and len(qty.VERB) then SEL.CMD := " SAMPLE ":qty.VERB

    clearselect 9

    if b.VERBOSE then
    crt "Selecting VOC local F items"
    crt "CMD = ":SEL.CMD
    execute SEL.CMD
    end else
    execute SEL.CMD capturing exop
    end

    b.SL9.ACTIVE = selectinfo(9,1)
    qty.SEL = 0
    if b.SL9.ACTIVE then qty.SEL = selectinfo(9,3)

    if b.VERBOSE then
    crt " Select list 9 Active? ":b.SL9.ACTIVE
    crt " Qty Selected = ":qty.SEL
    end

    if b.SL9.ACTIVE then
    SQL.CMD = 'SELECT @ID FMT "25L" AS "VOCNAME", '
    SQL.CMD := 'EVAL "@RECORD<2>" FMT "25L" AS "DATAFILE", '
    SQL.CMD := 'EVAL "@RECORD<3>" FMT "25L" AS "DICTFILE" '
    SQL.CMD := 'FROM VOC SLIST 9 '
    SQL.CMD := 'ORDER BY VOCNAME ASC'

    if @TTY[1,2] = "uv" then
    * UCI connections have @TTY='uvsrv'
    * Build the record set for the UCI connection
    ST = SQLExecDirect(@HSTMT, SQL.CMD)
    end else
    * Run this interactively
    perform SQL.CMD:";"
    end

    end

    @SYSTEM.RETURN.CODE = qty.SEL

    This can be run at TCL like this:
    TEST.SQLCALL LIMIT 200
    or
    TEST.SQLCALL LIMIT 1000

    It can be used in an SQL call with the following SQL statement:
    CALL TEST.SQLCALL LIMIT 200
    or 
    CALL TEST.SQLCALL LIMIT 200

    I think the BASIC program's syntax is something you will need to fine tune for UniData.
    The SQL syntax should be usable as is.

    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions Pty Ltd
    Mount Waverley VIC AU
    ------------------------------
    Gregor:

    Thanks for the sample program! I created that on our UV system and ran it but our system complains about the ORDER BY clause in the SQL statement. I get the following error "UniVerse/SQL: syntax error. Unexpected filename. Token was "ORDER". Scanned command was FROM VOC SLIST 9 ORDER".  If I remove the order by clause it works is there a setting somewhere that affects this?

    On another note I was able to get the SAMPLE keyword to work from outside by changing the setting on the DSN in windows to Retry as Universe SQL = yes.

    Thanks again for the help!

    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------
    Neil Morris
    Rocketeer
    Dec 22, 2021 2:41 PM UTC
    Gregor:

    Thanks for the sample program! I created that on our UV system and ran it but our system complains about the ORDER BY clause in the SQL statement. I get the following error "UniVerse/SQL: syntax error. Unexpected filename. Token was "ORDER". Scanned command was FROM VOC SLIST 9 ORDER".  If I remove the order by clause it works is there a setting somewhere that affects this?

    On another note I was able to get the SAMPLE keyword to work from outside by changing the setting on the DSN in windows to Retry as Universe SQL = yes.

    Thanks again for the help!

    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------
    The error indicates that the account may have a file named ORDER which overwrote the standard VOC entry keyword for ORDER?

    >CT VOC ORDER

    ORDER
    0001 K
    0002 511

    ------------------------------
    Neil Morris
    Universe Advanced Technical Support
    Rocket Software
    ------------------------------
    JR Moore
    JR MooreAuthor
    Participating Frequently
    Dec 22, 2021 2:47 PM UTC
    The error indicates that the account may have a file named ORDER which overwrote the standard VOC entry keyword for ORDER?

    >CT VOC ORDER

    ORDER
    0001 K
    0002 511

    ------------------------------
    Neil Morris
    Universe Advanced Technical Support
    Rocket Software
    ------------------------------
    Neil:

    Great catch yes it does.

    Thanks JR Moore

    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------
    Participating Frequently
    Dec 22, 2021 6:38 PM UTC
    Can someone tell me the proper syntax for SQL statement to Universe. I have a large file that I need to fetch 10000 rows at a time from Universe that will update a remote AZURE DB daily. I don't have control over the AZURE site as EDA seems like a much better solution to keep the remote data current.

    I have tried:

    SELECT TOP 10000 * FROM FILE;
    SELECT * FROM FILE LIMIT 10000;
    SELECT * FROM FILE FETCH FIRST 10000;
    SELECT * FROM FILE WHERE ROWNUM < 10000;

    Thanks JR Moore


    ------------------------------
    JR Moore
    IT Special Projects
    Rocket Forum Shared Account
    Hillsboro US
    ------------------------------
    After reading through this thread, I'm guessing that you don't always want just the first 10K records, you are looking to 'chunk' the file.

    To achieve that, you'll also need an offset/skip.

    I'm not sure about Universe (couldn't find the reference guide to check), but this isn't directly supported in AQL on D3.  However; if you save the results to a list first, you can do the following:

    execute "sselect file";
    execute "save-list listname";
    
    *first 10K
    execute "get-list listname (1-10000)"
    
    *next 10K
    execute "get-list listname (10001-20000)"​


    The overhead of having to select and save the list beforehand is not optimal, but I'm not aware of any ... SAMPLING {N} SKIP {N} - which would be a really nice addition.

    Hope this helps.

    Jeremy



    ------------------------------
    Jeremy Lockwood
    Awesome
    ASE Supply Inc
    Portland OR US
    ------------------------------