Chapter 36. DBMS_SQL

Table of Contents

36.1. Overview
36.2. Types
36.2.1. DESC_REC
36.2.2. DESC_REC2
36.2.3. DESC_TAB
36.2.4. DESC_TAB2
36.2.5. VARCHAR2A
36.2.6. DATE_TABLE
36.2.7. NUMBER_TABLE
36.2.8. VARCHAR2_TABLE
36.3. Procedures
36.3.1. BIND_ARRAY
36.3.2. BIND_VARIABLE
36.3.3. CLOSE_CURSOR
36.3.4. COLUMN_VALUE
36.3.5. DEFINE_COLUMN
36.3.6. DESCRIBE_COLUMNS
36.3.7. DESCRIBE_COLUMNS2
36.3.8. PARSE
36.4. Functions
36.4.1. EXECUTE
36.4.2. EXECUTE_AND_FETCH
36.4.3. FETCH_ROWS
36.4.4. OPEN_CURSOR
36.4.5. IS_OPEN
36.4.6. LAST_ERROR_POSITION
36.4.7. LAST_ROW_COUNT

This chapter briefly introduces the DBMS_SQL package, and describes how to use the procedures and functions of the package.

36.1. Overview

DBMS_SQL provides an interface to use dynamic SQL statements such as Data Manipulation Language(DML) and Data Definition Language(DDL).

A dynamic SQL statement is parsed as a string during PSM execution instead of being embedded in the source program. This enables the user to execute SELECT or DDL statements, including CREATE TABLE and DROP TABLE, on a table that did not exist when the PSM was created.

A dynamic SQL statement can be executed using the DBMS_SQL package as follows:

  1. Open a cursor with the OPEN_CURSOR function, and get the cursor ID.

  2. Pass the cursor ID to another function as a parameter to identify the cursor.

  3. Call the PARSE function to parse the dynamic SQL statement. If the statement contains a variable to be bound, invoke the BIND_VARIABLE function to bind the variable.

  4. Call the DEFINE_COLUMN function to define the result column type before retrieving the result.

  5. Execute the parsed SQL statement using the EXECUTE function.

  6. Fetch the result by executing the FETCH_ROWS function. To assign the fetched result to an output variable, call the COLUMN_VALUE function.

  7. If the cursor is no longer needed, close the cursor by executing the CLOSE_CURSOR function.

36.2. Types

This section describes the types provided by the DBMS_SQL package, in alphabetical order.

36.2.1. DESC_REC

The component types of the DESC_TAB type that is used with the DESCRIBE_COLUMNS procedure to retrieve column information.

Details about the DESC_REC type are as follows:

  • Prototype

    TYPE DESC_REC IS RECORD 
    (
        col_type             binary_integer,
        col_max_len          binary_integer,
        col_name             varchar2(32)  ,
        col_name_len         binary_integer,
        col_schema_name      varchar2(32)  ,
        col_schema_name_len  binary_integer,
        col_precision        binary_integer,
        col_scale            binary_integer,
        col_charsetid        binary_integer,
        col_charsetform      binary_integer,
        col_null_ok          boolean
    );

36.2.2. DESC_REC2

The component types of the DESC_TAB2 type that is used with the DESCRIBE_COLUMNS2 procedure to retrieve column information.

Details about the DESC_REC2 type are as follows:

  • Prototype

    TYPE DESC_REC2 IS RECORD 
    (
        col_type             binary_integer,
        col_max_len          binary_integer,
        col_name             varchar2(32767),
        col_name_len         binary_integer,
        col_schema_name      varchar2(32), 
        col_schema_name_len  binary_integer,
        col_precision        binary_integer,
        col_scale            binary_integer,
        col_charsetid        binary_integer,
        col_charsetform      binary_integer,
        col_null_ok          boolean);
    );

36.2.3. DESC_TAB

An array type that is used with the DESCRIBE_COLUMNS procedure to retrieve column information.

Details about the DESC_TAB type are as follows:

  • Prototype

    TYPE DESC_TAB IS TABLE OF DESC_REC INDEX BY BINARY_INTEGER;
    

36.2.4. DESC_TAB2

An array type that is used with the DESCRIBE_COLUMNS2 procedure to retrieve column information.

Details about the DESC_TAB2 type are as follows:

  • Prototype

    TYPE DESC_TAB2 IS TABLE OF DESC_REC2 INDEX BY BINARY_INTEGER;
    

36.2.5. VARCHAR2A

A table type that stores VARCHAR2 type data.

It is used to input a long SQL statement in the DBMS_SQL.PARSE procedure.

The following describes the VARCHAR2A type.

  • Prototype

    TYPE VARCHAR2A IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER;

36.2.6. DATE_TABLE

A table type that stores DATE type data.

The following describes the DATE_TABLE type.

  • Prototype

    TYPE DATE_TABLE IS TABLE OF DATE INDEX BY BINARY_INTEGER;

36.2.7. NUMBER_TABLE

A table type that stores NUMBER type data.

The following describes the NUMBER_TABLE type.

  • Prototype

    TYPE NUMBER_TABLE IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;

36.2.8. VARCHAR2_TABLE

A table type that stores VARCHAR2 type data.

The following describes the VARCHAR2 type.

  • Prototype

    TYPE VARCHAR2_TABLE IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;

36.3. Procedures

This section describes procedures provided by the DBMS_SQL package, in alphabetical order.

36.3.1. BIND_ARRAY

Binds a set of values to a variable that starts with a colon (:) in the SQL statement associated with the specified cursor. If the EXECUTE function is called without specifying all variable values, an exception occurs. Since a variable can be looked up by its name, the variable name in the SQL statement must be same as the name set to the parameter.

The BIND_ARRAY procedure can be used to insert, delete, or update multiple rows into a table in a single execution.

Details about the BIND_ARRAY procedure are as follows:

  • Prototype

    The BIND_ARRAY procedure can be overloaded if the variable is an index-organized table type (NUMBER, VARCHAR2, or DATE).

    • NUMBER_TABLE

      PROCEDURE BIND_ARRAY
      (
          c        IN     INTEGER, 
          name     IN     VARCHAR2, 
          n_tab    IN     NUMBER_TABLE [, 
          index1   IN     PLS_INTEGER, 
          index2   IN     PLS_INTEGER]
      ); 
    • VARCHAR2_TABLE

      PROCEDURE BIND_ARRAY
      (
          c        IN     INTEGER, 
          name     IN     VARCHAR2, 
          n_tab    IN     VARCHAR2_TABLE [, 
          index1   IN     PLS_INTEGER, 
          index2   IN     PLS_INTEGER]
      );
    • DATE_TABLE

      PROCEDURE BIND_ARRAY
      (
           c        IN     INTEGER, 
           name     IN     VARCHAR2, 
           n_tab    IN     DATE_TABLE [, 
           index1   IN     PLS_INTEGER, 
           index2   IN     PLS_INTEGER]
      );
  • Parameter

    ParameterDescription
    cTarget cursor.
    nameName of the variable in the SQL statement. The first character, the colon(:), can be omitted.
    n_tabSet of variables in the SQL statement.
    index1Start position in the index-organized table for binding. ( ≥ 1)
    index2End position in the index-organized table for binding. ( ≥ index1)
  • Example

    DECLARE
        stmt VARCHAR2(200);
        deptno_array DBMS_SQL.NUMBER_TABLE;
        name_array DBMS_SQL.VARCHAR2_TABLE;
        c NUMBER;
        dummy NUMBER;
    begin
        deptno_array(1) := 10; 
        deptno_array(2) := 20;
        deptno_array(3) := 30; 
        deptno_array(4) := 40;
        deptno_array(5) := 50; 
        deptno_array(6) := 60;
        name_array(1) := 'Architecture';
        name_array(2) := 'Frontend';
        name_array(3) := 'Core';
        name_array(4) := 'Management';
        name_array(5) := 'QMS';
        name_array(6) := 'Technical Support';
    
        stmt := 'insert into dept values(:deptno, :name)';
        c := DBMS_SQL.OPEN_CURSOR;
    
        DBMS_SQL.PARSE(c, stmt, DBMS_SQL.NATIVE);
        DBMS_SQL.BIND_ARRAY(c, ':deptno', deptno_array);
        DBMS_SQL.BIND_ARRAY(c, ':name', name_array, 2, 5);
        dummy := DBMS_SQL.EXECUTE(c);
    
        DBMS_SQL.CLOSE_CURSOR(c); 
    END;
    /

36.3.2. BIND_VARIABLE

Binds a value to a variable that starts with a colon(:) in the SQL statement associated with the specified cursor. If the EXECUTE function is called without specifying the variable value, an exception occurs. Since a variable can be looked up by its name, the variable name in the SQL statement must be same as the name set to the parameter.

Details about the BIND_VARIABLE procedure are as follows:

  • Prototype

    The BIND_VARIABLE procedure can be overloaded if the variable type is a NUMBER, VARCHAR2, DATE, TIMESTAMP, INTERVAL, BINARY_FLOAT, BINARY_DOUBLE, CLOB, BLOB, or CHAR.

    • NUMBER

      PROCEDURE BIND_VARIABLE
      (
          c      IN   INTEGER, 
          name   IN   VARCHAR2, 
          value  IN   NUMBER
      ); 
    • VARCHAR2

      PROCEDURE BIND_VARIABLE
      (
          c      IN   INTEGER, 
          name   IN   VARCHAR2, 
          value  IN   VARCHAR2[, OUT_VALUE_SIZE IN INTEGER]
      );
    • DATE

      PROCEDURE BIND_VARIABLE
      (
          c      IN   INTEGER, 
          name   IN   VARCHAR2, 
          value  IN   DATE
      );
    • TIMESTAMP

      PROCEDURE BIND_VARIABLE
      (
          c       IN   INTEGER, 
          name    IN   VARCHAR2, 
          value   IN   TIMESTAMP_UNCONSTRAINED
      );
    • INTERVAL

      PROCEDURE BIND_VARIABLE
      (
          C       IN   INTEGER, 
          NAME    IN   VARCHAR2, 
          VALUE   IN   YMINTERVAL_UNCONSTRAINED
      );
      PROCEDURE BIND_VARIABLE
      (
          c       IN   INTEGER, 
          name    IN   VARCHAR2, 
          value   IN   DSINTERVAL_UNCONSTRAINED
      );
    • BINARY_FLOAT

      PROCEDURE BIND_VARIABLE
      (
          c       IN   INTEGER, 
          name    IN   VARCHAR2, 
          value   IN   BINARY_FLOAT
      );
    • BINARY_DOUBLE

      PROCEDURE BIND_VARIABLE
      (
          c       IN   INTEGER, 
          name    IN   VARCHAR2, 
          value   IN   BINARY_DOUBLE
      );
    • CLOB

      PROCEDURE BIND_VARIABLE
      (
          c       IN   INTEGER, 
          name    IN   VARCHAR2, 
          value   IN   CLOB
      );
    • BLOB

      PROCEDURE BIND_VARIABLE
      (
          c       IN   INTEGER, 
          name    IN   VARCHAR2, 
          value   IN   BLOB
      );

    A separate procedure must be used for the CHAR, RAW, and ROWID types.

    • CHAR

      PROCEDURE BIND_VARIABLE_CHAR
      (
          c      IN   INTEGER, 
          name   IN   VARCHAR2,
          value  IN   CHAR[, out_value_size IN INTEGER]
      );
    • RAW

      PROCEDURE BIND_VARIABLE_RAW
      (
          c      IN   INTEGER, 
          name   IN   VARCHAR2,
          value  IN   RAW[, out_value_size IN INTEGER]
      );
    • ROWID

      PROCEDURE BIND_VARIABLE_ROWID
      (
          c      IN   INTEGER, 
          name   IN   VARCHAR2,
          value  IN   ROWID
      );
  • Parameter

    ParameterDescription
    cTarget cursor.
    nameName of the variable in the SQL statement. The first character, the colon(:), can be omitted.
    valueVariable value.
    out_value_sizeThis function is currently not supported.
  • Example

    DECLARE 
        csr INTEGER;
        x NUMBER := 1;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select :var from dual', dbms_sql.native); 
    
        DBMS_SQL.BIND_VARIABLE(csr, 'var', x); 
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /

36.3.3. CLOSE_CURSOR

Closes the given cursor and sets it to NULL.

Details about the CLOSE_CURSOR procedure are as follows:

  • Prototype

    PROCEDURE CLOSE_CURSOR
    (
        c IN OUT INTEGER
    );
  • Parameter

    ParameterDescription
    cCloses the target cursor and sets it to NULL.
  • Example

    DECLARE 
        csr INTEGER := DBMS_SQL.OPEN_CURSOR(); 
    BEGIN
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /

36.3.4. COLUMN_VALUE

Retrieves a column value fetched from the given cursor, and stores it in a variable. The value is fetched using the FETCH_ROWS function, and the COLUMN_VALUE procedure is used to retrieve the fetched data.

The column parameter of DEFINE_COLUMN must be the same type as that of the value parameter of COLUMN_VALUE. For instance, to call the DEFINE_COLUMN procedure whose column parameter type is NUMBER, the value parameter type of COLUMN_VALUE must also be NUMBER.

Since there are multiple procedure declarations with the same name but different parameter types, implicit type conversion must be taken into consideration. This means that the parameters of the invoked functions must be of the same type, but the actual variable types may be different. For example, DEFINE_COLUMN_CHAR must be used with COLUMN_VALUE_CHAR.

Details about the COLUMN_VALUE procedure are as follows:

  • Prototype

    COLUMN_VALUE procedures are overloaded for the variable types NUMBER, VARCHAR2, DATE, TIMESTAMP, INTERVAL, BINARY_FLOAT, BINARY_DOUBLE, CLOB, BLOB, or CHAR.

    • NUMBER

      PROCEDURE COLUMN_VALUE
      (
          c            IN    INTEGER, 
          position     IN    INTEGER, 
          value        OUT   NUMBER
      );
    • VARCHAR2

      PROCEDURE COLUMN_VALUE
      (
          c            IN    INTEGER, 
          position     IN    INTEGER, 
          value        OUT   VARCHAR2
      );
    • DATE

      PROCEDURE COLUMN_VALUE
      (
          c            IN    INTEGER, 
          position     IN    INTEGER, 
          value        OUT   DATE
      );
    • TIMESTAMP

      PROCEDURE COLUMN_VALUE
      (    
          c             IN   INTEGER, 
          position      IN   INTEGER, 
          value         OUT  TIMESTAMP_UNCONSTRAINED
      );
    • INTERVAL

      PROCEDURE COLUMN_VALUE
      (
          c             IN   INTEGER, 
          position      IN   INTEGER, 
          value         OUT  YMINTERVAL_UNCONSTRAINED
      );
      PROCEDURE COLUMN_VALUE
      (
          c             IN    INTEGER, 
          position      IN    INTEGER, 
          value         OUT   DSINTERVAL_UNCONSTRAINED
      );
    • BINARY_FLOAT

      PROCEDURE COLUMN_VALUE
      (
          c             IN    INTEGER, 
          position      IN    INTEGER, 
          value         OUT   BINARY_FLOAT
      );
    • BINARY_DOUBLE

      PROCEDURE COLUMN_VALUE
      (
          c             IN    INTEGER, 
          position      IN    INTEGER, 
          value         OUT   BINARY_DOUBLE
      );
    • BLOB

      PROCEDURE COLUMN_VALUE
      (
          c IN INTEGER,
          position IN INTEGER,
          value  OUT  BLOB
      );
    • CLOB

      PROCEDURE COLUMN_VALUE
      (
          c IN INTEGER,
          position IN INTEGER,
          value  OUT  CLOB
      );

    A separate procedure must be used for the CHAR, RAW, and ROWID types.

    • CHAR

      PROCEDURE COLUMN_VALUE_CHAR
      (
          c          IN   INTEGER, 
          position   IN   INTEGER, 
          value      OUT  CHAR[, column_error OUT NUMBER, 
                                actual_length OUT INTEGER]
      );
    • RAW

      PROCEDURE COLUMN_VALUE_RAW
      (
          c          IN   INTEGER, 
          position   IN   INTEGER, 
          value      OUT  RAW[, column_error OUT NUMBER, 
                              actual_length OUT INTEGER]
      );
    • ROWID

      PROCEDURE COLUMN_VALUE_ROWID
      (
          c          IN   INTEGER, 
          position   IN   INTEGER, 
          value      OUT  ROWID[, column_error OUT NUMBER]
      );
  • Parameter

    ParameterDescription
    cTarget cursor.
    positionRelative position of the column in the column list. Must be greater than or equal to 1.
    valueVariable in which to store the fetched column.
    column_error
    • If the column is NULL, 1405 is returned.

    • If the column is truncated, 1406 is returned.

    actual_lengthLength of the fetched column.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    INSERT INTO PSM_TABLE VALUES('111');
    
    DECLARE 
        csr INTEGER;
        col VARCHAR2(2);
        val VARCHAR2(2);
        ret INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select * from psm_table', dbms_sql.native); 
    
        /* result value is truncated */
        DBMS_SQL.DEFINE_COLUMN(csr, 1, col, 2); 
    
        ret := DBMS_SQL.EXECUTE(csr);
        ret := DBMS_SQL.FETCH_ROWS(csr);
    
        DBMS_SQL.COLUMN_VALUE(csr, 1, val); 
    
        DBMS_OUTPUT.PUT_LINE('val=' || val || '.');
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    val=11.

36.3.5. DEFINE_COLUMN

Defines the column type to fetch from the given cursor. This procedure can only be used in a SELECT statement.

Details about the DEFINE_COLUMN procedure are as follows:

  • Prototype

    DEFINE_COLUMN procedures are overloaded for the variable types NUMBER, VARCHAR2, DATE, TIMESTAMP, INTERVAL, BINARY_FLOAT, BINARY_DOUBLE, CLOB, and BLOB.

    • NUMBER

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   NUMBER
      );
    • VARCHAR2

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   VARCHAR2, 
          column_size    IN   INTEGER
      );
    • DATE

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   DATE
      );
    • TIMESTAMP

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   TIMESTAMP_UNCONSTRAINED
      );
    • INTERVAL

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   YMINTERVAL_UNCONSTRAINED
      );
      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   DSINTERVAL_UNCONSTRAINED
      );
    • BINARY_FLOAT

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN    BINARY_FLOAT
      );
    • BINARY_DOUBLE

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   BINARY_DOUBLE
      );
    • CLOB

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   CLOB
      );
    • BLOB

      PROCEDURE DEFINE_COLUMN
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   BLOB
      );

    A separate procedure must be used for the CHAR, RAW, and ROWID types.

    • CHAR

      PROCEDURE DEFINE_COLUMN_CHAR
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   CHAR, 
          column_size    IN   INTEGER
      );
    • RAW

      PROCEDURE DEFINE_COLUMN_RAW
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   RAW, 
          column_size    IN   INTEGER
      );
    • ROWID

      PROCEDURE DEFINE_COLUMN_ROWID
      (
          c              IN   INTEGER, 
          position       IN   INTEGER, 
          column         IN   ROWID
      );
  • Parameter

    ParameterDescription
    cTarget cursor.
    position

    Relative position of the column in the column list.

    The first position of the column in the statement is 1. Must be greater than or equal to 1.

    columnColumn variable. Only the type is referenced.
    column_size

    Maximum length of the column value to execute SELECT on VARCHAR2 or CHAR data.

    If the column value is longer than the specified length, it is truncated.

  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    INSERT INTO PSM_TABLE VALUES('abc');
    
    DECLARE 
        csr INTEGER;
        v VARCHAR2(1);
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select * from psm_table', dbms_sql.native); 
    
        DBMS_SQL.DEFINE_COLUMN(csr, 1, v, 1); 
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /

36.3.6. DESCRIBE_COLUMNS

Displays the column types of the result table from the open cursor. This procedure can be used in a SELECT statement after specifying the SELECT statement using the PARSE procedure.

Details about the DESCRIBE_COLUMNS procedure are as follows:

  • Prototype

    PROCEDURE DESCRIBE_COLUMNS
    (
        c              IN    INTEGER, 
        col_cnt        OUT   INTEGER,
        desc_t         OUT   DESC_TAB
    );
  • Parameter

    ParameterDescription
    cTarget cursor.
    col_cntNumber of columns in the SELECT result table.
    desc_tList (desc_tab) of information (desc_rec) about each column.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    INSERT INTO PSM_TABLE VALUES('abc');
    
    DECLARE 
        csr INTEGER;
        col_cnt INTEGER;
        rec_tab DBMS_SQL.DESC_TAB;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select * from psm_table', dbms_sql.native); 
    
        DBMS_SQL.DESCRIBE_COLUMNS(csr, col_cnt, rec_tab); 
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /

36.3.7. DESCRIBE_COLUMNS2

Displays the column types of the result table from the open cursor. This procedure can be used in a SELECT statement after specifying the SELECT statement using the PARSE procedure.

While DESCRIBE_COLUMNS only displays up to 32 bytes of the column name, this procedure can display up to 32767 bytes.

Details about the DESCRIBE_COLUMNS2 procedure are as follows:

  • Prototype

    PROCEDURE DESCRIBE_COLUMNS2
    (
        c              IN    INTEGER, 
        col_cnt        OUT   INTEGER,
        desc_t         OUT   DESC_TAB2
    );
  • Parameter

    ParameterDescription
    cTarget cursor.
    col_cntNumber of columns in the SELECT result table.
    desc_tList (desc_tab2) of information (desc_rec2) about each column.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    INSERT INTO PSM_TABLE VALUES('abc');
    
    DECLARE 
        csr INTEGER;
        col_cnt INTEGER;
        rec_tab DBMS_SQL.DESC_TAB2;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select * from psm_table', dbms_sql.native); 
    
        DBMS_SQL.DESCRIBE_COLUMNS2(csr, col_cnt, rec_tab); 
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /

36.3.8. PARSE

Parses a given SQL statement. If the statement is a DDL statement, it is immediately parsed. If another SQL statement is open, it will be closed and the specified SQL statement will be parsed.

In general, a SQL statement is executed as VARCHAR2 data type string. CLOB or VARCHAR2A data type is used to create long text strings.

Details about the PARSE procedure are as follows:

  • Prototype

    • VARCHAR2

      PROCEDURE PARSE
      (
          c               IN    INTEGER, 
          statement       IN    VARCHAR2, 
          language_flag   IN    INTEGER DEFAULT NULL
      );
    • CLOB

      PROCEDURE PARSE
      (
          c               IN    INTEGER, 
          statement       IN    CLOB, 
          language_flag   IN    INTEGER DEFAULT NULL
      );
    • VARCHAR2A

      PROCEDURE PARSE
      (
          c               IN    INTEGER, 
          statement       IN    VARCHAR2A, 
          lb              IN    INTEGER, 
          ub              IN    INTEGER,
          lfflg           IN    BOOLEAN, 
          language_flag   IN    INTEGER DEFAULT NULL
      );
  • Parameter

    ParameterDescription
    cTarget cursor.
    statement

    SQL statement to parse.

    Unlike a PSM statement, a SQL statement does not need to be terminated by a semicolon(;).

    lbLower bound for elements in the VARCHAR2A type statement.
    ubUpper bound for elements in the VARCHAR2A type statement.
    lfflgIf true, a linefeed is inserted after each VARCHAR2A data type element.
    language_flagThis function is currently not supported.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    
    DECLARE 
        csr INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select * from psm_table', dbms_sql.native); 
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    
    DECLARE
        csr INTEGER;
        sql_arr DBMS_SQL.VARCHAR2A;
        r number;
        trec PSM_TABLE%ROWTYPE;
        lb number;
        ub number;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        sql_arr(1) := 'insert ';
        sql_arr(2) := 'into ';
        sql_arr(3) := 'PSM_TABLE ';
        sql_arr(4) := 'values';
        sql_arr(5) := '(';
        sql_arr(6) := '''abc''';
        sql_arr(7) := ')';
    
        lb := 1;
        ub := 7;
        DBMS_SQL.PARSE(csr, sql_arr, lb, ub, true, dbms_sql.native);
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END;
    /

36.4. Functions

This section describes the functions provided by the DBMS_SQL package, in alphabetical order.

36.4.1. EXECUTE

Executes a given cursor. For an INSERT, UPDATE, or DELETE statement, the number of processed rows is returned.

Details about the EXECUTE function are as follows:

  • Prototype

    FUNCTION EXECUTE
    (
        c    IN    INTEGER
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    cTarget cursor.
  • Return Value

    ValueDescription
    INTEGERReturns the number of processed rows for an INSERT, UPDATE, or DELETE statement.
    UNDEFINEDReturned for statements other than an INSERT, UPDATE, or DELETE statement.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR(3));
                
    DECLARE 
        csr INTEGER;
        ret INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'insert into psm_table values(''abc'')', 
                       dbms_sql.native); 
    
        ret := DBMS_SQL.EXECUTE(csr);
    
        DBMS_OUTPUT.PUT_LINE('affected row cnt = ' || ret || '.');
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    affected row cnt = 1.

36.4.2. EXECUTE_AND_FETCH

Same as calling EXECUTE followed by FETCH_ROWS on a given cursor. Returns the same value as FETCH_ROWS, which is the number of fetched rows.

Details about the EXECUTE_AND_FETCH function are as follows:

  • Prototype

    FUNCTION EXECUTE_AND_FETCH
    (
        c      IN   INTEGER, 
        exact  IN   BOOLEAN DEFAULT FALSE
    )
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    cTarget cursor.
    exact
    • TRUE: Raises an exception unless exactly one row is fetched.

    • FALSE: Does not raise an exception even if multiple rows are fetched.

  • Return Value

    ValueDescription
    0Returned if there are no rows to fetch.
    1Returned if the fetch was successful.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR(3));
    INSERT INTO PSM_TABLE VALUES('abc');
                
    DECLARE 
        csr INTEGER;
        ret INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'SELECT * FROM PSM_TABLE', dbms_sql.native); 
    
        ret := DBMS_SQL.EXECUTE_AND_FETCH(csr);
        DBMS_OUTPUT.PUT_LINE('fetched row cnt = ' || ret || '.');
    
        LOOP 
            ret := DBMS_SQL.FETCH_ROWS(csr);
            DBMS_OUTPUT.PUT_LINE('fetched row cnt = ' || ret || '.');
            exit when ret = 0;
        END LOOP;
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    fetched row cnt = 1.
    fetched row cnt = 0.

36.4.3. FETCH_ROWS

Fetches a row from a given cursor. Returns the number of fetched rows. If this function is called when there are no more rows to fetch, an exception occurs.

Details about the FETCH_ROWS function are as follows:

  • Prototype

    FUNCTION FETCH_ROWS
    (
        c   IN   INTEGER
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    cTarget cursor.
  • Return Value

    ValueDescription
    0Returned if there are no rows to fetch.
    1Returned if the fetch was successful.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    INSERT INTO PSM_TABLE VALUES('abc');
                
    DECLARE 
        csr INTEGER;
        ret INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'SELECT * FROM PSM_TABLE', dbms_sql.native); 
    
        ret := DBMS_SQL.EXECUTE(csr);
        LOOP 
            ret := DBMS_SQL.FETCH_ROWS(csr);
            DBMS_OUTPUT.PUT_LINE('fetched row cnt = ' || ret || '.');
            exit when ret = 0;
        END LOOP;
    
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    fetched row cnt = 1.
    fetched row cnt = 0.

36.4.4. OPEN_CURSOR

Opens a new cursor to execute a SQL statement. CLOSE_CURSOR must be called when the open cursor is no longer needed and the resources must be returned to the server.

An open cursor can execute a SQL statement multiple times or another SQL statement. If a cursor is successfully opened, the function returns an INTEGER type cursor ID. The value can be used for a cursor parameter in the DBMS_SQL package.

Details about the OPEN_CURSOR function are as follows:

  • Prototype

    FUNCTION OPEN_CURSOR() 
    RETURN INTEGER;
  • Example

    DECLARE 
        csr INTEGER; 
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    END; 
    /

36.4.5. IS_OPEN

Checks whether or not the given cursor can be opened with the DBMS_SQL.open_cursor function.

Details about the IS_OPEN function are as follows:

  • Prototype

    FUNCTION IS_OPEN
    (
        c   IN   INTEGER
    ) 
    RETURN BOOLEAN;
  • Parameter

    ParameterDescription
    cTarget cursor.
  • Return Value

    ValueDescription
    trueReturned if the cursor is open.
    falseReturned if the cursor is not open.
  • Example

    DECLARE 
        csr INTEGER := DBMS_SQL.OPEN_CURSOR(); 
        is_open BOOLEAN;
    BEGIN
        is_open := DBMS_SQL.IS_OPEN(csr); 
    
        if is_open then
            DBMS_OUTPUT.PUT_LINE('opened.');
        end if;
    END; 
    /
    opened.

36.4.6. LAST_ERROR_POSITION

Returns the position of an error that occurred while parsing. This function must be called immediately after the PARSE procedure is called.

Details about the LAST_ERROR_POSITION function are as follows:

  • Prototype

    FUNCTION LAST_ERROR_POSITION return INTEGER;
  • Return Value

    ValueDescription
    INTEGERReturns the offset where the error occurred starting from 0.
  • Example

    DECLARE 
        csr INTEGER; 
        position INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'select * from psm_not_exist_table', dbms_sql.native); 
    
    EXCEPTION 
    WHEN OTHERS THEN
        position := DBMS_SQL.LAST_ERROR_POSITION;
    
        DBMS_OUTPUT.PUT_LINE('error position = ' || position);
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    error position = 14

36.4.7. LAST_ROW_COUNT

Returns the total number of fetched rows in the currently executing statement. This function must be called immediately after the PARSE procedure is called.

Details about the LAST_ROW_COUNT function are as follows:

  • Prototype

    FUNCTION LAST_ROW_COUNT return INTEGER;
  • Return Value

    ValueDescription
    INTEGERReturns the cumulative number of fetched rows in the currently executing statement.
  • Example

    CREATE TABLE PSM_TABLE (A VARCHAR2(3));
    INSERT INTO PSM_TABLE VALUES('111');
    INSERT INTO PSM_TABLE VALUES('222');
                
    DECLARE 
        csr INTEGER;
        ret INTEGER;
        cnt INTEGER;
    BEGIN
        csr := DBMS_SQL.OPEN_CURSOR(); 
    
        DBMS_SQL.PARSE(csr, 'SELECT * FROM PSM_TABLE', dbms_sql.native); 
    
        ret := DBMS_SQL.EXECUTE(csr);
        LOOP 
            ret := DBMS_SQL.FETCH_ROWS(csr);
            exit when ret = 0;
        END LOOP;
    
        cnt := DBMS_SQL.LAST_ROW_COUNT;
    
        DBMS_OUTPUT.PUT_LINE('last row count = ' || cnt);
        DBMS_SQL.CLOSE_CURSOR(csr); 
    END; 
    /
    last row count = 2