Dynamic SQL Statement is Not Anonymous Block or CALL Statement
--==============================================
Repeated Placeholder Names in Dynamic SQL Statements
--==============================================
If you repeat placeholder names in dynamic SQL statements, be aware that the way
placeholders are associated with bind variables depends on the kind of dynamic SQL
statement.
Topics
1. Dynamic SQL Statement is Not Anonymous Block or CALL Statement
2. Dynamic SQL Statement is Anonymous Block or CALL Statement
--==============================================
• Dynamic SQL Statement is Not Anonymous Block or CALL Statement
--==============================================
If the dynamic SQL statement does not represent an anonymous PL/SQL block or a CALL statement,
then repetition of placeholder names is insignificant.
Placeholders are associated with bind variables in the USING clause by position, not by name.
For example:- in this dynamic SQL statement, the repetition of the name :x is insignificant:
sql_stmt := 'INSERT
INTO payroll VALUES (:x, :x, :y, :x)';
In the corresponding USING clause, you must supply four bind variables. They can be different;
for example like :- EXECUTE IMMEDIATE sql_stmt USING a, b, c, d;
DROP table XX_TEMP_TL
create table XX_TEMP_TL (
SUB_1 NUMBER (3),
SUB_2 NUMBER (3),
SUB_3 NUMBER (3),
SUB_4 NUMBER (3)
)
select * from XX_TEMP_TL
DECLARE
a NUMBER := 1;
b NUMBER := 2;
c NUMBER := 3;
d NUMBER := 4;
sql_stmt VARCHAR2(100);
BEGIN
sql_stmt := 'INSERT
INTO XX_TEMP_TL VALUES (:x, :x, :y, :x)';
EXECUTE IMMEDIATE sql_stmt USING a, b, c, d;
dbms_output.put_line ('total
no of rows inserted is : '|| SQL%ROWCOUNT );
END;
select * from XX_TEMP_TL
SUB_1 SUB_2 SUB_3 SUB_4
1 2 3 4
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
If you wann to associate the same bind variable with each occurrence of :x,
Then you have to repeat that bind variable;
for example:
EXECUTE IMMEDIATE sql_stmt USING a, a, b, a;
The preceding EXECUTE IMMEDIATE statement runs this SQL statement:
sql_stmt := 'INSERT
INTO payroll VALUES (a, a, b, a)';
DECLARE
a NUMBER := 1;
b NUMBER := 2;
c NUMBER := 3;
d NUMBER := 4;
sql_stmt VARCHAR2(100);
BEGIN
sql_stmt := 'INSERT
INTO XX_TEMP_TL VALUES (:x, :x, :y, :x)';
EXECUTE IMMEDIATE sql_stmt USING a, a, b, a;
dbms_output.put_line ('total
no of rows inserted is : '|| SQL%ROWCOUNT );
END;
select * from XX_TEMP_TL
SUB_1 SUB_2 SUB_3 SUB_4
1 1 2 1
Comments
Post a Comment