简体   繁体   中英

Explanation about a code fragment

I see the following code fragment in a legacy application that accesses Informix through C. Can anyone explain what the SQL in the code is trying to achieve? Thanks.

EXEC SQL BEGIN DECLARE SECTION;
int i_tableref;
EXEC SQL END DECLARE SECTION;

    /* Some code here */

if (!i_sel_ref)
{
    exec sql begin declare section;
    const char *sql1 = 
        "select refer_num.nextval from table ( SET{''} )";
    exec sql end declare section;
    exec sql prepare oref_sel_fid from :sql1;
    if ( sqlca.sqlcode != SQL_OK )
    {
         /* some code */
    }
    /* More code */
}

I believe it is obtaining the next value from a database sequence called refer_num. Sequences are a way of generating values for numeric unique identifiers - a bit like IDENTITY columns in some DBMSs. I don't know Informix, but my guess is that "table ( SET{''} )" is a way of generating a pseudo-table with 1 row so that you can perform a select statement that doesn't actually need to access any real database table. Oracle has a special table called DUAL for this purpose, and this would be a common sight in Oracle:

select refer_num.nextval from dual;

for getting next value of a sequence generator in Informix:

select your_seq_generator_name.nextval from table(set{1});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM