Entering Data into Tables
The following sections explain how to perform cursor-independent operations and update and delete using cursors.
The SQL statements CALL, INSERT, DELETE and UPDATE, as well user-defined function invocations, embedded in application programs operate on a set of rows in a table or view in exactly the same way as in interactive SQL.
Host variables may be used in the statements to supply values or set search conditions. Host variables may be used as routine parameters.
Examples:exec sql UPDATE BOOKADM.FREEROOMS SET FREECOUNT = FREECOUNT-1 WHERE HOTELCODE = :HOTCOD AND ROOMTYPE = :BOOK_ROOMT AND ON_DATE >= :BOOK_ARRIVE AND ON_DATE < :BOOK_DEPART; exec sql CALL ASK_FREEROOMS(:HOTCOD, :BOOK_ROOMT, CURRENT_DATE, CURRENT_DATE + INTERVAL '7' DAY, :FREEROOM_COUNT);
From the standpoint of the application program, each statement is a single indivisible operation, regardless of how many columns and rows are affected.
Updating and Deleting Through Cursors
The UPDATE CURRENT and DELETE CURRENT statements, see the Mimer SQL Reference Manual for the syntax description, allow update and delete operations respectively, to be controlled on a row-by-row basis from an application program. These statements operate through cursors, which are declared and opened as described above for FETCH.
These statements operate on the current row of the cursor referenced in the statement. If there is no current row, e.g. the cursor has been opened but not yet positioned with a FETCH statement, an error is raised.
UPDATE CURRENT changes the content of the current row according to the SET clause in the statement, but does not change the position of the cursor. Two consecutive UPDATE CURRENT statements will therefore update the same row twice.
DELETE CURRENT deletes the current row and does not move the cursor; after a DELETE CURRENT statement, the cursor is positioned "between rows" and there is no current row. The cursor must be moved to the next row with a FETCH statement before any other operation can be performed through the cursor.
For both UPDATE CURRENT and DELETE CURRENT statements, the table name as used in the statement must be exactly the same as the table name addressed in the cursor declaration. The cursor must also address an updatable result set.
If a FOR UPDATE OF clause is used to specify which fetched columns may be updated, only the columns specified may appear in the corresponding UPDATE statement.
Cursors are not updatable if the data retrieval statement in the cursor declaration contains any of the following features at the top level (i.e. not in a subselect) of the statement:
- reference to more than one table in the FROM clause
- reference to a read-only view in the FROM clause
- the keyword DISTINCT
- set-functions in the SELECT list (AVG, COUNT, MAX, MIN, SUM)
- arithmetic or string concatenation expressions in the SELECT list
- a GROUP BY clause
- an ORDER BY clause
- the UNION keyword
- the result set of an explicit inner or outer JOIN
- a CALL to a result set procedure
When to Use UPDATE CURRENT, DELETE CURRENT
UPDATE CURRENT and DELETE CURRENT statements are useful for manipulating single rows in interactive applications where rows are displayed, and the user decides which rows to delete or update.
The example below illustrates the program framework for such an operation (the construction is similar for a DELETE CURRENT operation):exec sql BEGIN DECLARE SECTION; ... exec sql END DECLARE SECTION; ... exec sql DECLARE C1 CURSOR FOR ... ; ... exec sql OPEN C1; exec sql WHENEVER NOT FOUND GOTO done; loop exec sql FETCH C1 INTO :var1,:var2,...,:varn; display var1,var2,...,varn; prompt "Update this row?"; if answer = "yes" then prompt "Give new values"; exec sql UPDATE tab SET col1 = :newval1, col2 = :newval2, ... WHERE CURRENT OF C1; display "row updated"; end if; prompt "Display next row?"; if answer = "no" then exit; end if; end loop; done: exec sql CLOSE C1;
In situations where there is no requirement to interactively choose rows and where all the rows to be updated or deleted can be specified completely in terms of a WHERE clause, it is more efficient to do so rather than use a cursor.
An operation completely specified as a WHERE clause is executed as a single statement, rather than a series of statements (i.e. one for each FETCH etc.).
Upright Database Technology AB
Voice: +46 18 780 92 00
Fax: +46 18 780 92 40