The DBMS_SQL system package provides a mechanism for executing dynamic statements, supporting various types of statements: system commands (such as ALTER SESSION), DDL, and DML (including SELECT).
Applicability
This content applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL-compatible mode.
Consider the following points:
Only DML statements can have parameters specified.
Only statements that return a result set can have their results fetched in DML statements.
DML statements are executed in interfaces such as
dbms_sql.execute, while other statements are executed in thedbms_sql.parseinterface.Cursors created by
DBMS_SQLare only available within the current session and cannot be accessed by other sessions.
DBMS_SQL Privileges
The DBMS_SQL package is owned by the SYS user and is compiled by AUTHID CURRENT_USER. Any DBMS_SQL subprogram called from an anonymous PL block runs with the privileges of the current user.
DBMS_SQL constants
The following table lists the constants used for the language_flag parameter in the PARSE stored procedure of DBMS_SQL.
Name |
Type |
Value |
Description |
|---|---|---|---|
| NATIVE | INTEGER | 1 | The program is connected to the database normally. |
Usage instructions
The following usage instructions describe how to perform queries, updates, inserts, and deletes.
If you use dynamic SQL to process queries, you must perform the following steps:
Specify the variables for the return values of the
SELECTstatement by calling theDEFINE_COLUMNsubroutine.Execute the
SELECTstatement by calling theEXECUTEfunction.Call the
FETCH_ROWSfunction to retrieve the rows that meet the query criteria.Call the
COLUMN_VALUEsubprogram to determine the column values retrieved by the query returned by theFETCH_ROWSfunction.
If you use dynamic SQL to handle INSERT, UPDATE, or DELETE statements, you must execute the INSERT, UPDATE, or DELETE statement by calling the EXECUTE function.
Execution process of DBMS_SQL
The execution process of DBMS_SQL is as follows:
OPEN_CURSORPARSEBIND_VARIABLEDEFINE_COLUMN,DEFINE_COLUMN_LONG, orDEFINE_ARRAYEXECUTEFETCH_ROWSorEXECUTE_AND FETCHCOLUMN_VALUECLOSE_CURSOR
Overview of DBMS_SQL Subprograms
The following table lists the DBMS_SQL subprograms supported by the current version of OceanBase Database and provides brief descriptions.
Subprogram |
Description |
|---|---|
| BIND_VARIABLE | Binds a specified value to a specified variable. |
| COLUMN_VALUE | Returns the value of the specified element in the cursor. |
| CLOSE_CURSOR | Close the specified cursor and release memory. |
| DEFINE_COLUMN | Defines the columns to select by specifying a cursor. This clause is used only forSELECTstatement. |
| DEFINE_ARRAY | Defines the set of rows to be selected from the specified cursor, used only forSELECTstatement. |
| DESCRIBE_COLUMNS | Retrieves attributes of the cursor, such as all field names and types. |
| EXECUTE | Executes the specified cursor. |
| EXECUTE_AND_FETCH | Execute the specified cursor and fetch a row. |
| FETCH_ROWS | Retrieves a row of data from the specified cursor. |
| IS_OPEN | If the specified cursor status isOPEN, it returnsTRUE. |
| LAST_ERROR_POSITION | Get the last call.DBMS_SQL.PARSEThe position where the syntax error occurred. |
| OPEN_CURSOR | Returns the ID of the new cursor. |
| PARSE | Parse the specified statement. |
| VARIABLE_VALUE | This function is used to obtain the values of output variables after executing dynamic SQL statements. |
