Firebird Documentation IndexFirebird 2.0 Language Ref. UpdateDML statements → EXECUTE BLOCK
Firebird Home Firebird Home Prev: DELETEFirebird Documentation IndexUp: DML statementsNext: EXECUTE PROCEDURE

EXECUTE BLOCK

Available in: DSQL

Added in: 2.0

Description: Executes a block of PSQL code as if it were a stored procedure, optionally with input and output parameters and variable declarations. This allows the user to perform “on-the-fly” PSQL within a DSQL context.

Syntax: 

EXECUTE BLOCK [(<inparams>)]
     [RETURNS (<outparams>)]
AS
   [<declarations>]
BEGIN
   [<PSQL statements>]
END

<inparams>      ::=  paramname type = ? [, <inparams>]
<outparams>     ::=  paramname type [, <outparams>]
<declarations>  ::=  See PSQL::DECLARE for the exact syntax

Examples: 

This example injects the numbers 0 through 127 and their corresponding ASCII characters into the table ASCIITABLE:

execute block
as
declare i int = 0;
begin
  while (i < 128) do
  begin
    insert into AsciiTable values (:i, ascii_char(:i));
    i = i + 1;
  end
end

The next example calculates the geometric mean of two numbers and returns it to the user:

execute block (x double precision = ?, y double precision = ?)
returns (gmean double precision)
as
begin
  gmean = sqrt(x*y);
  suspend;
end

Because this block has input parameters, it has to be prepared first. Then the parameters can be set and the block executed. It depends on the client software how this must be done and even if it is possible at all – see the notes below.

Our last example takes two integer values, smallest and largest. For all the numbers in the range smallest .. largest, the block outputs the number itself, its square, its cube and its fourth power.

execute block (smallest int = ?, largest int = ?)
returns (number int, square bigint, cube bigint, fourth bigint)
as
begin
  number = smallest;
  while (number <= largest) do
  begin
    square = number * number;
    cube   = number * square;
    fourth = number * cube;
    suspend;
    number = number + 1;
  end
end

Again, it depends on the client software if and how you can set the parameter values.

Notes: 

Prev: DELETEFirebird Documentation IndexUp: DML statementsNext: EXECUTE PROCEDURE
Firebird Documentation IndexFirebird 2.0 Language Ref. UpdateDML statements → EXECUTE BLOCK