0% found this document useful (0 votes)
12 views10 pages

Dynamic SQL with DBMS_SQL in PL/SQL

This document provides examples of using dynamic SQL in PL/SQL with the DBMS_SQL package. It demonstrates how to: 1. Parse SQL statements and bind variables using DBMS_SQL.PARSE and DBMS_SQL.BIND_VARIABLE. 2. Fetch results from a query using DBMS_SQL.FETCH_ROWS and store in variables with DBMS_SQL.COLUMN_VALUE. 3. Pass multiple bind variables using DBMS_SQL.BIND_ARRAY and bind_tables for numbers and strings. 4. Define column datatypes and sizes for fetches using DBMS_SQL.DEFINE_COLUMN. 5. Retrieve values of outbind variables using DB
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Dynamic SQL with DBMS_SQL in PL/SQL

This document provides examples of using dynamic SQL in PL/SQL with the DBMS_SQL package. It demonstrates how to: 1. Parse SQL statements and bind variables using DBMS_SQL.PARSE and DBMS_SQL.BIND_VARIABLE. 2. Fetch results from a query using DBMS_SQL.FETCH_ROWS and store in variables with DBMS_SQL.COLUMN_VALUE. 3. Pass multiple bind variables using DBMS_SQL.BIND_ARRAY and bind_tables for numbers and strings. 4. Define column datatypes and sizes for fetches using DBMS_SQL.DEFINE_COLUMN. 5. Retrieve values of outbind variables using DB
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Examples

execute anystring(create table emp(name varchar2 , age number(3), salary


number(20)));



1 10G PL/SQL
Using USING clause
USING clause is used along with EXECUTE IMMEDIATE statement
to pass parameter as Bind variable.

Example:
Declare
Symbol varchar(4) := WOND;
Begin
Execute immediate delete from stock where symbol = :symbol
Using Symbol;
End;


2 10G PL/SQL
Using RETURNING INTO clause
RETURNING INTO clause allows us to return column values for rows
affected by DML statements.

Example:
Declare
Emp_id number := 1001;
Salary_Bonus_percent number := 10;
New_salary number;
Begin
Execute immediate Update emp set salary = salary + (salary*
: Salary_Bonus_percent ) where emp_id = :emp_id returning salary into
:New_salary
using Salary_Bonus_percent , emp_id
Returning into New_salary;
End;


3 10G PL/SQL
Using DBMS_SQL
Prior to Oracle 9i , Dynamic SQL required the use of DBMS_SQL package
which gives greated control over the processing flow with in the
Dynamic SQL, but it is generally more complicated to write than the
native dynamic SQL.

Create or replace procedure anystring(string IN varchar2) is
Begin
Cursor_name integer;
Ret integer;
Cursor_name := DBMS_SQL.open_cursor;
DBMS_SQL.parse(cursor_name , string,DBMS_SQL.Native);
Ret := DBMS_SQL.execute(cursor_name);
DBMS_SQL.close_cursor(cursor_name);
End;


4 10G PL/SQL
Using DBMS_SQL
Execute anystring (drop table CD);

Execute anystring(create table CD (Artist varchar2(25).Title varchar2(25));

Execute anystring(Insert into CD values (MLTR,Sleeping Child));

Using BIND_VARIABLE and BIND_ARRAY procedures:

This procedures are used in passing parameter as Bind variables to dynamic sql
statements.BIND_ARRAY is used to pass more than one parameter.

Usage:-


5 10G PL/SQL
Using DBMS_SQL
Usage:-

DBMS_SQL.Parse(cursor_name,delete from CD where Artist = :artist,DBMS_SQL.Native);
DBMS_SQL.BIND_VARIABLE(cursor_name,:artist,artist_name);

Where artist_name is a parameter which is already declared or passed from calling
environment.

Use BIND_ARRAY To pass more than one parameter.
Usage:-

Declare
cursor_name Integer := dbms_sql.open_cursor;
Ret Integer;
emp_num dbms_sql.number_table;
emp_var dbms_sql.varchar2_table;





6 10G PL/SQL
Using DBMS_SQL
begin
dbms_sql.parse(cursor_name,'insert into emp(emp_id,emp_name) values ( :a, :b
)',dbms_sql.native );
emp_num(1) := 1001;
emp_num(2) := 1002;
emp_var(1) := 'John';
emp_var(2) := 'Naveen';
dbms_sql.bind_array(cursor_name, ':a', emp_num );
dbms_sql.bind_array(cursor_name, ':b', emp_var );
Ret := dbms_sql.execute(cursor_name);
dbms_sql.close_cursor(cursor_name);
end;





7 10G PL/SQL
Using DBMS_SQL
Using DEFINE_COLUMN function:
If your cursor performs a query, you must execute this function once for each column
being [Link] it basically used to define the datatype and size of the variables that
will receive data from the Fetch_Rows() function


If the column defined with LONG datatype , then DEFINE_COLUMN_LONG must be
used.
Usage:-
.
DBMS_SQL.parse(cursor_name, select artist,title from CD, dbms_sql.native);
DBMS_SQL.DEFINE_COLUMN(cursor_name,1,artist,25);
DBMS_SQL.DEFINE_COLUMN(cursor_name,2,title,25);
.

8 10G PL/SQL
Using DBMS_SQL
Using FETCH_ROWS:
This function fetches a single row of data into the local buffer. This data can
then be stored in local variables by using the Column_Value() procedure.

Ret := DBMS_SQL.FETCH_ROWS(cursor_name);

Using COLUMN_VALUE:
This procedure stores the fetched single row of data into local variables.

DBMS_SQL.COLUMN_VALUE(cursor_name,1,artist);
DBMS_SQL.COLUMN_VALUE(cursor_name,2,title);

9 10G PL/SQL
Using DBMS_SQL
Using VARIABLE_VALUE:

BEGIN
cursor_name := dbms_sql.open_cursor;
dbms_sql.parse(cursor_name, 'INSERT INTO emp VALUES
(:empid,:salary,:bonus_percent) ' ||
'returning salary+(salary*bonus_percent) into :new_salary', dbms_sql.native);

dbms_sql.bind_variable(cursor_name, 'empid', v_empid);
dbms_sql.bind_variable(cursor_name, 'salary', v_salary);
dbms_sql.bind_variable(cursor_name, 'bonus_percent', v_bonus_percent);
dbms_sql.bind_variable(cursor_name, 'new_salary', v_new_salary);
n := dbms_sql.execute(c);
dbms_sql.variable_value(cursor_name, 'new_salary', v_new_salary); -- get value
of outbind
dbms_sql.close_cursor(c);
END;


10 10G PL/SQL

Common questions

Powered by AI

The VARIABLE_VALUE procedure in DBMS_SQL is used to retrieve the value of output bind variables after execution of a SQL statement that includes an OUT parameter, such as when a value is returned from a stored procedure or a DML operation with a RETURNING clause. This allows developers to capture and utilize results produced during dynamic SQL execution, facilitating further processing or decision-making based on these outputs .

In PL/SQL, cursors are used to hold and process query results row by row. The OPEN_CURSOR function within the DBMS_SQL package initializes a new cursor in preparation for parsing and executing a SQL statement. This function allocates a cursor number, which serves as a handle to uniquely identify the cursor during its lifecycle. It is a critical first step in leveraging the DBMS_SQL package for executing dynamic SQL, enabling developers to perform subsequent operations like parsing, binding, and executing SQL commands on the server .

In DBMS_SQL, DEFINE_COLUMN is used to define the structure of the data to be fetched, indicating each column's expected type and size. After executing a query and using FETCH_ROWS to retrieve a row, COLUMN_VALUE is applied to extract the actual data into local PL/SQL variables based on the definitions provided earlier by DEFINE_COLUMN. This combination allows programmers to handle dynamically retrieved data effectively by ensuring that each piece of data is correctly typed and stored for further manipulation .

The BIND_ARRAY procedure in the DBMS_SQL package allows for binding an entire array of values as parameters in one operation, which is invaluable when handling batch processes or multiple similar SQL operations in a single execution cycle. By processing multiple parameters simultaneously, BIND_ARRAY improves performance by minimizing context switches between PL/SQL and SQL engines. This is particularly crucial when inserting a large number of rows, enabling efficient bulk binding, which is a major performance-enhancing feature .

The FETCH_ROWS function within the DBMS_SQL package fetches a single row of query results into a local buffer, providing a mechanism to sequentially retrieve each row from the results of an executed SQL query. This function allows for fine-grained control over the retrieval process, permitting detailed handling and manipulation of each row's data, especially useful when working with large datasets where step-by-step processing is needed .

In the DBMS_SQL package, the DEFINE_COLUMN function is essential for specifying the data type and size of a column when fetching data within a dynamic SQL context. It is used before retrieving the actual data to inform the system about the expected data structure, ensuring that the fetched data fits into appropriate PL/SQL variable types. This function is critical for operations that work with data selection and retrieval, as it prepares the system to correctly handle the data types defined in the SQL query .

The USING clause in PL/SQL is employed along with the EXECUTE IMMEDIATE statement to pass parameters as bind variables. This approach enhances SQL query security and efficiency by allowing variables to be substituted for actual values, thereby avoiding SQL injection vulnerabilities and reducing hard parsing. For instance, in a scenario where the task is to delete records from a table based on certain conditions, using EXECUTE IMMEDIATE with the USING clause allows a variable to pass the condition securely .

The DBMS_SQL package provides more granular control over dynamic SQL execution in PL/SQL by allowing programmatic handling of cursors, parameter binding, and different execution flows. It offers functions like OPEN_CURSOR, PARSE, EXECUTE, FETCH_ROWS, and CLOSE_CURSOR which give developers the ability to define complex operations, such as multiple binds with BIND_VARIABLE and BIND_ARRAY procedures, and error handling in a dynamic SQL context. Although DBMS_SQL is more cumbersome compared to native dynamic SQL, it is preferred when detailed control over query execution is necessary, especially before Oracle 9i .

Dynamic SQL using the DBMS_SQL package is more complex and offers greater control compared to the simpler EXECUTE IMMEDIATE. DBMS_SQL allows for intricate control over cursor handling, parameter management, and can handle multiple SQL operations within a single execution cycle. It is particularly suited for scenarios requiring complex logic, such as conditional SQL execution or advanced error handling. On the other hand, EXECUTE IMMEDIATE is ideal for straightforward dynamic SQL operations due to its simplicity and improved performance with direct SQL execution paths and single-statement execution .

The RETURNING INTO clause in PL/SQL provides a mechanism to capture values from rows affected by DML operations (like INSERT, UPDATE, DELETE) directly into variables. This is particularly beneficial in dynamic SQL as it allows for immediate use of modified values within a PL/SQL block without needing a separate SELECT statement. For example, after updating an employee’s salary with a bonus, the RETURNING INTO clause can immediately store the new salary value into a variable for further processing .

You might also like