tags:

views:

99

answers:

1

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 */
}
+1  A: 

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;
Tony Andrews
Recent versions of IDS have a table with a name like `sysdual`. I create myself a table called `dual` when I create a database. The classic representation of a single element table is is '`SELECT refer_num.nextval FROM systables WHERE tabid = 1`' (or, if being pedantic so the code will work with MODE ANSI databases too, '`SELECT refer_num.nextval FROM "informix".systables WHERE tabid = 1`').
Jonathan Leffler
Also, in more recent versions of IDS (actually, any version of IDS that has sequences is recent enough), you could use EXECUTE IMMEDIATE to reduce the volume of code.
Jonathan Leffler
I have always preferred the 'table (set{1})' approach or the one that Jonathan mentions (using a tabid = 1 clause)since at least on one occasion one of our developers decided to add one more value to the custom sysdual table we had created, resulting in various pieces of the code bombing...
calvinkrishy