DEV Community

Pranav Bakare
Pranav Bakare

Posted on

DBMS_SQL package | Dynamic SQL

When using the DBMS_SQL package, there is a predefined flow or sequence of operations that you must follow in order to execute dynamic SQL effectively. These operations are essential for managing and executing dynamic queries, especially when working with cursors and retrieving results. Below is the standard flow:

Predefined Syntax and Flow of Operations in DBMS_SQL:

  1. Open a Cursor: Before anything, you need to open a cursor. This is the first step because you need a cursor to associate with the dynamic SQL.

v_cursor := DBMS_SQL.OPEN_CURSOR;

  1. Parse the SQL: Once you have the cursor, you need to parse the dynamic SQL. Parsing tells Oracle that you are about to execute a SQL statement, and Oracle will compile the SQL internally.

DBMS_SQL.PARSE(v_cursor, v_sql, DBMS_SQL.NATIVE);

Here, v_sql contains your dynamically constructed SQL query. The DBMS_SQL.NATIVE option indicates that the SQL is in native SQL (as opposed to PL/SQL or some other language).

  1. Define the Columns: If your query will return data (e.g., SELECT statements), you need to define the columns that will hold the results. This is where you tell Oracle how to bind the result set to a variable.

DBMS_SQL.DEFINE_COLUMN(v_cursor, column_position, variable);

column_position: The position of the column in the SELECT query (1 for the first column, 2 for the second, and so on).

variable: The PL/SQL variable that will hold the column value.

  1. Execute the SQL: After parsing the SQL and defining columns (if necessary), you execute the SQL using DBMS_SQL.EXECUTE_AND_FETCH. This step runs the query and fetches the results.

rows_processed := DBMS_SQL.EXECUTE_AND_FETCH(v_cursor);

Here, rows_processed will return the number of rows fetched by the query.

  1. Fetch Data: After execution, you can fetch the data from the cursor. This step is necessary if your query returns results, such as with SELECT queries.

DBMS_SQL.COLUMN_VALUE(v_cursor, column_position, variable);

  1. Close the Cursor: Once you are done with the cursor, always close it to free resources. This is crucial to avoid cursor leaks and to ensure proper resource management.

DBMS_SQL.CLOSE_CURSOR(v_cursor);

Example of the Full Flow:

Here's an example that demonstrates the entire process:

DECLARE
v_cursor INTEGER;
v_sql VARCHAR2(1000);
v_salary NUMBER;
v_emp_id NUMBER := 101; -- Example of dynamic bind variable
BEGIN
-- Step 1: Open the cursor
v_cursor := DBMS_SQL.OPEN_CURSOR;

-- Step 2: Construct and parse dynamic SQL
v_sql := 'SELECT salary FROM employees WHERE employee_id = :emp_id';
DBMS_SQL.PARSE(v_cursor, v_sql, DBMS_SQL.NATIVE);

-- Step 3: Define the columns to fetch
DBMS_SQL.DEFINE_COLUMN(v_cursor, 1, v_salary);

-- Step 4: Bind the variable (dynamic bind variable for employee ID)
DBMS_SQL.BIND_VARIABLE(v_cursor, ':emp_id', v_emp_id);

-- Step 5: Execute the query
IF DBMS_SQL.EXECUTE_AND_FETCH(v_cursor) > 0 THEN
    -- Step 6: Fetch the value of the column (salary)
    DBMS_SQL.COLUMN_VALUE(v_cursor, 1, v_salary);
    DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END IF;

-- Step 7: Close the cursor
DBMS_SQL.CLOSE_CURSOR(v_cursor);
Enter fullscreen mode Exit fullscreen mode

END;
/

Explanation of the Flow:

  1. Open the Cursor:
    A cursor is opened to manage the dynamic SQL execution.

  2. Parse the SQL:
    The dynamic SQL is parsed and compiled by Oracle. The v_sql string contains the query that is executed dynamically. We use DBMS_SQL.NATIVE because it is regular SQL.

  3. Define the Columns:
    Since we are selecting data from the employees table (specifically the salary column), we define the column we expect to fetch using DBMS_SQL.DEFINE_COLUMN. The first column in the result set will be stored in the v_salary variable.

  4. Bind the Variable:
    We bind the :emp_id bind variable to the v_emp_id PL/SQL variable using DBMS_SQL.BIND_VARIABLE. This allows us to dynamically pass the employee ID to the SQL query at runtime.

  5. Execute the Query:
    The query is executed using DBMS_SQL.EXECUTE_AND_FETCH, which processes the SQL statement and returns the number of rows fetched.

  6. Fetch Data:
    We use DBMS_SQL.COLUMN_VALUE to retrieve the value of the salary column from the result set and store it in the v_salary variable.

  7. Close the Cursor:
    Finally, after we are done with the cursor, we close it to release resources.


Summary:

Yes, when using DBMS_SQL, there is a predefined sequence of operations to follow:

  1. Open a cursor to hold the dynamic SQL.

  2. Parse the SQL query.

  3. Define the columns to bind the query results.

  4. Bind variables if necessary (e.g., for dynamic query parameters).

  5. Execute the query and fetch the results.

  6. Close the cursor after execution.

This flow ensures that the dynamic SQL is executed properly, resources are managed efficiently, and the results are fetched correctly.

.

Top comments (1)

Collapse
 
dvsrk profile image
dvsrk

Good writing, great if you add exception handling to it, what if the processing fails after a cursor is open and didn't go through clean exit path (closing the cursor especially). If a cursor is kept long open it would have negative effects wrt to various DB resources.