Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   SQL Statements > UPDATE >

Updating a Row Variable (IDS, ESQL/C)

The UPDATE statement with the Collection-Derived-Table segment allows you to update fields in a row variable. The Collection-Derived-Table segment identifies the row variable in which to update the fields. For more information, see Collection-Derived Table.

To update fields
  1. Create a row variable in your ESQL/C program.
  2. Optionally, select a ROW-type column into the row variable with the SELECT statement (without the Collection-Derived-Table segment).
  3. Update fields of the row variable with the UPDATE statement and the Collection-Derived-Table segment.
  4. After the row variable contains the correct fields, you then use the UPDATE or INSERT statement on a table or view name to save the row variable in the ROW column (named or unnamed).

The UPDATE statement and the Collection-Derived-Table segment allow you to update a field or a group of fields in the row variable. Specify the new field values in the SET clause. For example, the following UPDATE changes the x and y fields in the myrect ESQL/C row variable:

EXEC SQL BEGIN DECLARE SECTION;
   row (x int, y int, length float, width float) myrect;
EXEC SQL END DECLARE SECTION;
. . .
EXEC SQL select into :myrect from rectangles where area = 64;
EXEC SQL update table(:myrect) set x=3, y=4;

Suppose that after the SELECT statement, the myrect2 variable has the values x=0, y=0, length=8, and width=8. After the UPDATE statement, the myrect2 variable has field values of x=3, y=4, length=8, and width=8. You cannot use a row variable in the Collection-Derived-Table segment of an INSERT statement.

You can, however, use the UPDATE statement and the Collection-Derived-Table segment to insert new field values into a row host variable, if you specify a value for every field in the row.

For example, the following code fragment inserts new field values into the row variable myrect and then inserts this row variable into the database:

EXEC SQL update table(:myrect)
   set x=3, y=4, length=12, width=6;
EXEC SQL insert into rectangles
   values (72, :myrect);

If the row variable is an untyped variable, you must use a SELECT statement before the UPDATE so that ESQL/C can determine the data types of the fields. An UPDATE of fields in a row variable cannot include a WHERE clause.

The row variable can store the field values of the row, but it has no intrinsic connection with a database column. Once the row variable contains the correct field values, you must then save the variable into the ROW column with one of the following SQL statements:

For examples of SPL ROW variables, see the IBM Informix Guide to SQL: Tutorial. For more information on using ESQL/C row variables, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual.

Related Information

Related statements: DECLARE , INSERT , OPEN , SELECT, and FOREACH

For a task-oriented discussion of the UPDATE statement, see the IBM Informix Guide to SQL: Tutorial.

For a discussion of the GLS aspects of the UPDATE statement, see the IBM Informix GLS User's Guide.

For information on how to access row and collections with ESQL/C host variables, see the discussion of complex data types in the IBM Informix ESQL/C Programmer's Manual.

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]