Table of Contents
This chapter briefly introduces the DBMS_SQL package, and describes how to use the procedures and functions of the package.
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:
Open a cursor with the OPEN_CURSOR function, and get the cursor ID.
Pass the cursor ID to another function as a parameter to identify the cursor.
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.
Call the DEFINE_COLUMN function to define the result column type before retrieving the result.
Execute the parsed SQL statement using the EXECUTE function.
Fetch the result by executing the FETCH_ROWS function. To assign the fetched result to an output variable, call the COLUMN_VALUE function.
If the cursor is no longer needed, close the cursor by executing the CLOSE_CURSOR function.
This section describes the types provided by the DBMS_SQL package, in alphabetical order.
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 );
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); );
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;
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;
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;
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;
This section describes procedures provided by the DBMS_SQL package, in alphabetical order.
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
Parameter | Description |
---|---|
c | Target cursor. |
name | Name of the variable in the SQL statement. The first character, the colon(:), can be omitted. |
n_tab | Set of variables in the SQL statement. |
index1 | Start position in the index-organized table for binding. ( ≥ 1) |
index2 | End 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; /
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
Parameter | Description |
---|---|
c | Target cursor. |
name | Name of the variable in the SQL statement. The first character, the colon(:), can be omitted. |
value | Variable value. |
out_value_size | This 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; /
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
Parameter | Description |
---|---|
c | Closes the target cursor and sets it to NULL. |
Example
DECLARE csr INTEGER := DBMS_SQL.OPEN_CURSOR(); BEGIN DBMS_SQL.CLOSE_CURSOR(csr); END; /
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
Parameter | Description |
---|---|
c | Target cursor. |
position | Relative position of the column in the column list. Must be greater than or equal to 1. |
value | Variable in which to store the fetched column. |
column_error |
|
actual_length | Length 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.
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
Parameter | Description |
---|---|
c | Target 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. |
column | Column 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; /
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
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; /
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
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; /
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
Parameter | Description |
---|---|
c | Target cursor. |
statement | SQL statement to parse. Unlike a PSM statement, a SQL statement does not need to be terminated by a semicolon(;). |
lb | Lower bound for elements in the VARCHAR2A type statement. |
ub | Upper bound for elements in the VARCHAR2A type statement. |
lfflg | If true, a linefeed is inserted after each VARCHAR2A data type element. |
language_flag | This 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; /
This section describes the functions provided by the DBMS_SQL package, in alphabetical order.
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
Parameter | Description |
---|---|
c | Target cursor. |
Return Value
Value | Description |
---|---|
INTEGER | Returns the number of processed rows for an INSERT, UPDATE, or DELETE statement. |
UNDEFINED | Returned 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.
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
Parameter | Description |
---|---|
c | Target cursor. |
exact |
|
Return Value
Value | Description |
---|---|
0 | Returned if there are no rows to fetch. |
1 | Returned 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.
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
Parameter | Description |
---|---|
c | Target cursor. |
Return Value
Value | Description |
---|---|
0 | Returned if there are no rows to fetch. |
1 | Returned 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.
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; /
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
Parameter | Description |
---|---|
c | Target cursor. |
Return Value
Value | Description |
---|---|
true | Returned if the cursor is open. |
false | Returned 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.
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
Value | Description |
---|---|
INTEGER | Returns 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
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
Value | Description |
---|---|
INTEGER | Returns 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