Using RETURNING INTO Clause: RETURNING INTO Clause Allows Us To Return Column Values For Rows Affected by DML Statements
This document discusses using the DBMS_SQL package in Oracle to execute dynamic SQL statements. It describes how to open a cursor, parse a SQL statement, bind variables, define column types, fetch rows, and retrieve column values using DBMS_SQL procedures like BIND_VARIABLE, DEFINE_COLUMN, FETCH_ROWS, and COLUMN_VALUE.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
255 views
Using RETURNING INTO Clause: RETURNING INTO Clause Allows Us To Return Column Values For Rows Affected by DML Statements
This document discusses using the DBMS_SQL package in Oracle to execute dynamic SQL statements. It describes how to open a cursor, parse a SQL statement, bind variables, define column types, fetch rows, and retrieve column values using DBMS_SQL procedures like BIND_VARIABLE, DEFINE_COLUMN, FETCH_ROWS, and COLUMN_VALUE.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
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;
1 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;
2 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:-
3 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:-
4 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;
5 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 selected.Is 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); .
6 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.