DECLARE

<< CLOSE cursor | FB 2.0 Language Reference | EXCEPTION >>

DECLARE

Available in: PSQL

DECLARE ... CURSOR

Added in: 2.0

Description

Declares a named cursor and binds it to its own SELECT statement. The cursor can later be opened, used to walk the result set, and closed again. Positioned updates and deletes (using WHERE CURRENT OF) are also supported. PSQL cursors are available in triggers, stored procedures and EXECUTE BLOCK statements.

Syntax

 DECLARE [VARIABLE] cursorname CURSOR FOR (select-statement);

Example

 execute block
 returns (relation char(31), sysflag int)
 as
 declare cur cursor for
    (select rdb$relation_name, rdb$system_flag from rdb$relations);
 begin
    open cur;
    while (1=1) do
    begin
       fetch cur into relation, sysflag;
       if (row_count = 0) then leave;
       suspend;
    end
    close cur;
 end

Notes:

  • A FOR UPDATE clause is allowed in the SELECT statement, but not required for a positioned update or delete to succeed.
  • Make sure that declared cursor names do not clash with any names defined later on in AS CURSOR clauses.
  • If you need a cursor to loop through an output set, it is almost always easier – and less error-prone – to use a FOR SELECT statement with an AS CURSOR clause. Declared cursors must be explicitly opened, fetched from, and closed. Furthermore, you need to check row_count after every fetch and break out of the loop if it is zero. AS CURSOR takes care of all of that automatically. However, declared cursors give you more control over the sequence of events, and allow you to operate several cursors in parallel.
  • The SELECT statement may contain named SQL parameters, like in select name || :sfx from names where number = :num. Each parameter must be a PSQL variable that has been declared previously (this includes any in/out params of the PSQL module). When the cursor is opened, the parameter is assigned the current value of the variable.
  • Caution! If the value of a PSQL variable that is used in the SELECT statement changes during execution of the loop, the statement may (but will not always) be re-evaluated for the remaining rows. In general, this situation should be avoided. If you really need this behaviour, test your code thoroughly and make sure you know how variable changes affect the outcome. Also be advised that the behaviour may depend on the query plan, in particular the use of indices. As it is currently not strictly defined, it may change in some future version of Firebird.

See also:
OPEN cursor
FETCH cursor
CLOSE cursor

back to top of page

DECLARE [VARIABLE] with initialization

Changed in: 1.5

Description

In Firebird 1.5 and above, a PSQL local variable can be initialized upon declaration. The VARIABLE keyword has become optional.

Syntax

 DECLARE [VARIABLE] varname datatype [{= | DEFAULT} value];

Example

 create procedure proccie (a int)
 returns (b int)
 as
   declare p int;
   declare q int = 8;
   declare r int default 9;
   declare variable s int;
   declare variable t int = 10;
   declare variable u int default 11;
 begin
   <intelligent code here>
 end

back to top of page
<< CLOSE cursor | FB 2.0 Language Reference | EXCEPTION >>