6.8. EXECUTE BLOCK

Used for

Creating an “anonymous” block of PSQL code in DSQL for immediate execution

Available in

DSQL

Syntax

  1. EXECUTE BLOCK [(<inparams>)]
  2. [RETURNS (<outparams>)]
  3. AS
  4. [<declarations>]
  5. BEGIN
  6. [<PSQL statements>]
  7. END
  8. <inparams> ::= <param_decl> = ? [, <inparams> ]
  9. <outparams> ::= <param_decl> [, <outparams>]
  10. <param_decl> ::= paramname <type> [NOT NULL] [COLLATE collation]
  11. <type> ::= <datatype> | [TYPE OF] domain | TYPE OF COLUMN rel.col
  12. <datatype> ::=
  13. {SMALLINT | INTEGER | BIGINT}
  14. | {FLOAT | DOUBLE PRECISION}
  15. | {DATE | TIME | TIMESTAMP}
  16. | {DECIMAL | NUMERIC} [(precision [, scale])]
  17. | {CHAR | CHARACTER} [VARYING] | VARCHAR} [(size)]
  18. [CHARACTER SET charset]
  19. | {NCHAR | NATIONAL {CHARACTER | CHAR}} [VARYING] [(size)]
  20. | BLOB [SUB_TYPE {subtype_num | subtype_name}]
  21. [SEGMENT SIZE seglen] [CHARACTER SET charset]
  22. | BLOB [(seglen [, subtype_num])]
  23. <declarations> ::= <declare_item> [<declare_item> ...]
  24. <declare_item> ::= <declare_var>; | <declare_cursor>;
Table 78. Arguments for the EXECUTE BLOCK Statement Parameters
ArgumentDescription

param_decl

Name and description of an input or output parameter

declarations

A section for declaring local variables and named cursors

declare_var

Local variable declaration

declare_cursor

Declaration of a named cursor

paramname

The name of an input or output parameter of the procedural block, up to 31 characters long. The name must be unique among input and output parameters and local variables in the block

datatype

SQL data type

collation

Collation sequence

domain

Domain

rel

Name of a table or view

col

Name of a column in a table or view

precision

Precision. From 1 to 18

scale

Scale. From 0 to 18. It must be less than or equal to precision

size

The maximum size of a string, in characters

charset

Character set

subtype_num

BLOB subtype number

subtype_name

BLOB subtype mnemonic name

seglen

Segment size, it cannot be greater than 65,535

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.

Examples

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

  1. EXECUTE BLOCK
  2. AS
  3. declare i INT = 0;
  4. BEGIN
  5. WHILE (i < 128) DO
  6. BEGIN
  7. INSERT INTO AsciiTable VALUES (:i, ascii_char(:i));
  8. i = i + 1;
  9. END
  10. END

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

  1. EXECUTE BLOCK (x DOUBLE PRECISION = ?, y DOUBLE PRECISION = ?)
  2. RETURNS (gmean DOUBLE PRECISION)
  3. AS
  4. BEGIN
  5. gmean = SQRT(x*y);
  6. SUSPEND;
  7. 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.

  1. EXECUTE BLOCK (smallest INT = ?, largest INT = ?)
  2. RETURNS (number INT, square BIGINT, cube BIGINT, fourth BIGINT)
  3. AS
  4. BEGIN
  5. number = smallest;
  6. WHILE (number <= largest) DO
  7. BEGIN
  8. square = number * number;
  9. cube = number * square;
  10. fourth = number * cube;
  11. SUSPEND;
  12. number = number + 1;
  13. END
  14. END

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

6.8.1. Input and output parameters

Executing a block without input parameters should be possible with every Firebird client that allows the user to enter his or her own DSQL statements. If there are input parameters, things get trickier: these parameters must get their values after the statement is prepared but before it is executed. This requires special provisions, which not every client application offers. (Firebird’s own isql, for one, doesn’t.)

The server only accepts question marks (“?”) as placeholders for the input values, not “:a”, “:MyParam” etc., or literal values. Client software may support the “:xxx” form though, and will preprocess it before sending it to the server.

If the block has output parameters, you must use SUSPEND or nothing will be returned.

Output is always returned in the form of a result set, just as with a SELECT statement. You can’t use RETURNING_VALUES or execute the block INTO some variables, even if there is only one result row.

PSQL Links

For more information about parameter and variable declarations, and <PSQL statements> consult Chapter 7, Procedural SQL (PSQL) Statements.

For <declarations> in particular, see DECLARE [VARIABLE] and DECLARE CURSOR for the exact syntax.

6.8.2. Statement Terminators

Some SQL statement editors — specifically the isql utility that comes with Firebird and possibly some third-party editors — employ an internal convention that requires all statements to be terminated with a semi-colon. This creates a conflict with PSQL syntax when coding in these environments. If you are unacquainted with this problem and its solution, please study the details in the PSQL chapter in the section entitled Switching the Terminator in isql.