7.6.13. OPEN

Used for

Opening a declared cursor

Available in

PSQL

Syntax

  1. OPEN cursorname
Table 90. OPEN Statement Parameter
ArgumentDescription

cursorname

Cursor name. A cursor with this name must be previously declared with a DECLARE CURSOR statement

An OPEN statement opens a previously declared cursor, executes the SELECT statement declared for it and makes the first record of the result data set ready to fetch. OPEN can be applied only to cursors previously declared in a DECLARE VARIABLE statement.

If the SELECT statement declared for the cursor has parameters, they must be declared as local variables or exist as input or output parameters before the cursor is declared. When the cursor is opened, the parameter is assigned the current value of the variable.

Examples

  1. Using the OPEN statement:

    1. SET TERM ^;
    2. CREATE OR ALTER PROCEDURE GET_RELATIONS_NAMES
    3. RETURNS (
    4. RNAME CHAR(31)
    5. )
    6. AS
    7. DECLARE C CURSOR FOR (
    8. SELECT RDB$RELATION_NAME
    9. FROM RDB$RELATIONS);
    10. BEGIN
    11. OPEN C;
    12. WHILE (1 = 1) DO
    13. BEGIN
    14. FETCH C INTO :RNAME;
    15. IF (ROW_COUNT = 0) THEN
    16. LEAVE;
    17. SUSPEND;
    18. END
    19. CLOSE C;
    20. END^
    21. SET TERM ;^
  2. A collection of scripts for creating views using a PSQL block with named cursors:

    1. EXECUTE BLOCK
    2. RETURNS (
    3. SCRIPT BLOB SUB_TYPE TEXT)
    4. AS
    5. DECLARE VARIABLE FIELDS VARCHAR(8191);
    6. DECLARE VARIABLE FIELD_NAME TYPE OF RDB$FIELD_NAME;
    7. DECLARE VARIABLE RELATION RDB$RELATION_NAME;
    8. DECLARE VARIABLE SOURCE TYPE OF COLUMN RDB$RELATIONS.RDB$VIEW_SOURCE;
    9. -- named cursor
    10. DECLARE VARIABLE CUR_R CURSOR FOR (
    11. SELECT
    12. RDB$RELATION_NAME,
    13. RDB$VIEW_SOURCE
    14. FROM
    15. RDB$RELATIONS
    16. WHERE
    17. RDB$VIEW_SOURCE IS NOT NULL);
    18. -- named cursor with local variable
    19. DECLARE CUR_F CURSOR FOR (
    20. SELECT
    21. RDB$FIELD_NAME
    22. FROM
    23. RDB$RELATION_FIELDS
    24. WHERE
    25. -- Important! The variable shall be declared earlier
    26. RDB$RELATION_NAME = :RELATION);
    27. BEGIN
    28. OPEN CUR_R;
    29. WHILE (1 = 1) DO
    30. BEGIN
    31. FETCH CUR_R
    32. INTO :RELATION, :SOURCE;
    33. IF (ROW_COUNT = 0) THEN
    34. LEAVE;
    35. FIELDS = NULL;
    36. -- The CUR_F cursor will use
    37. -- variable value of RELATION initialized above
    38. OPEN CUR_F;
    39. WHILE (1 = 1) DO
    40. BEGIN
    41. FETCH CUR_F
    42. INTO :FIELD_NAME;
    43. IF (ROW_COUNT = 0) THEN
    44. LEAVE;
    45. IF (FIELDS IS NULL) THEN
    46. FIELDS = TRIM(FIELD_NAME);
    47. ELSE
    48. FIELDS = FIELDS || ', ' || TRIM(FIELD_NAME);
    49. END
    50. CLOSE CUR_F;
    51. SCRIPT = 'CREATE VIEW ' || RELATION;
    52. IF (FIELDS IS NOT NULL) THEN
    53. SCRIPT = SCRIPT || ' (' || FIELDS || ')';
    54. SCRIPT = SCRIPT || ' AS ' || ASCII_CHAR(13);
    55. SCRIPT = SCRIPT || SOURCE;
    56. SUSPEND;
    57. END
    58. CLOSE CUR_R;
    59. END

See also

DECLARE CURSOR, FETCH, CLOSE