The DBMS_SQL package provides a mechanism for executing dynamic statements. It supports various types of statements, including system commands (such as ALTER SESSION), Data Definition Language (DDL), and Data Manipulation Language (DML) statements, including SELECT.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
Consider the following points when using DBMS_SQL:
Only DML statements can specify parameters.
Only DML statements that return result sets can be retrieved.
DML statements are executed in
dbms_sql.executeinterfaces, while other statements are executed indbms_sql.parseinterfaces.Cursors created by
DBMS_SQLare only available in the current session and cannot be accessed by other sessions.
Privilege requirements
DBMS_SQL is a system package owned by the SYS user and compiled with AUTHID CURRENT_USER. Any subprogram of DBMS_SQL called from an anonymous PL/SQL block runs with the privileges of the current user.
Constants
The following table lists the constants used by the language_flag parameter of the PARSE stored procedure in DBMS_SQL.
| Name | Type | Value | Description |
|---|---|---|---|
| NATIVE | INTEGER | 1 | Indicates that the program is connected to the database normally. |
Considerations
The following considerations describe how to handle queries, updates, inserts, and deletes.
If you use dynamic SQL to handle a query, you must perform the following steps:
Use the
DEFINE_COLUMNsubprogram to specify variables for the return values of theSELECTstatement.Use the
EXECUTEfunction to run theSELECTstatement.Use the
FETCH_ROWSfunction to retrieve rows that meet the query criteria.Use the
COLUMN_VALUEsubprogram to determine the column values retrieved by theFETCH_ROWSfunction.
If you use dynamic SQL to handle an INSERT, UPDATE, or DELETE statement, you must use the EXECUTE function to run the statement.
Execution process
The execution process of DBMS_SQL is as follows:
OPEN_CURSORPARSEBIND_VARIABLEDEFINE_COLUMN,DEFINE_COLUMN_LONG, orDEFINE_ARRAYEXECUTEFETCH_ROWSorEXECUTE_AND_FETCHCOLUMN_VALUECLOSE_CURSOR
Subprograms
The following table lists the DBMS_SQL subprograms supported in the current version of OceanBase Database and their brief descriptions.
| Subprogram | Description |
|---|---|
| BIND_VARIABLE | Binds a specified value to a specified variable. |
| COLUMN_VALUE | Returns the value of a specified element in the cursor. |
| CLOSE_CURSOR | Closes a specified cursor and releases memory. |
| DEFINE_COLUMN | Defines a column selected by a specified cursor, applicable only to SELECT statements. |
| DEFINE_ARRAY | Defines a collection to be selected from a specified cursor, applicable only to SELECT statements. |
| DESCRIBE_COLUMNS | Retrieves all field names and types of a cursor. |
| EXECUTE | Executes a specified cursor. |
| EXECUTE_AND_FETCH | Executes a specified cursor and retrieves rows. |
| FETCH_ROWS | Retrieves a row of data from a specified cursor. |
| IS_OPEN | Returns TRUE if the specified cursor is OPEN. |
| LAST_ERROR_POSITION | Retrieves the position where a syntax error occurred in the last call to DBMS_SQL.PARSE. |
| OPEN_CURSOR | Returns the ID of a new cursor. |
| PARSE | Parses a specified statement. |
| VARIABLE_VALUE | Retrieves the value of an output variable after executing a dynamic SQL statement. |
