demonstrates the effect of duplicate placeholders with EXECUTE IMMEDIATE. : Execute Immediate « PL SQL « Oracle PL / SQL






demonstrates the effect of duplicate placeholders with EXECUTE IMMEDIATE.

  
SQL>
SQL>
SQL> CREATE TABLE duplicates (
  2    f1 NUMBER,
  3    f2 NUMBER,
  4    f3 NUMBER,
  5    f4 NUMBER);

Table created.

SQL>
SQL> DECLARE
  2    v_InsertStmt VARCHAR2(100);
  3    v_f1 NUMBER := 1;
  4    v_f2 NUMBER := 2;
  5    v_f3 NUMBER := 3;
  6  BEGIN
  7    v_InsertStmt :=
  8      'INSERT INTO duplicates VALUES (:a, :b, :c, :a)';
  9
 10    EXECUTE IMMEDIATE v_InsertStmt
 11      USING v_f1, v_f2, v_f3, v_f1;
 12  END;
 13  /

PL/SQL procedure successfully completed.

SQL>
SQL> SELECT *
  2    FROM duplicates;

        F1         F2         F3         F4
---------- ---------- ---------- ----------
         1          2          3          1

1 row selected.

SQL>
SQL> DECLARE
  2    v_InsertStmt VARCHAR2(100);
  3    v_f1 NUMBER := 4;
  4    v_f2 NUMBER := 5;
  5    v_f3 NUMBER := 6;
  6  BEGIN
  7    v_InsertStmt :=
  8      'BEGIN ' ||
  9      '  INSERT INTO duplicates VALUES (:a, :b, :c, :a); ' ||
 10      'END;';
 11
 12    EXECUTE IMMEDIATE v_InsertStmt
 13      USING v_f1, v_f2, v_f3;
 14  END;
 15  /

PL/SQL procedure successfully completed.

SQL>
SQL> SELECT *
  2    FROM duplicates;

        F1         F2         F3         F4
---------- ---------- ---------- ----------
         1          2          3          1
         4          5          6          4

2 rows selected.

SQL>
SQL> DROP TABLE duplicates;

Table dropped.

SQL>
SQL>

   
  








Related examples in the same category

1.EXECUTE IMMEDIATE commands
2.Execute immediate for an insert statement
3.Use EXECUTE IMMEDIATE to call procedure and save the returning value
4.Use execute immediate to insert random numbers
5.execute immediate with 'insert ... using' to pass variable into insert statement
6.Timing Bind variable
7.Call 'execute immediate' to drop table, create table and insert data
8.EXECUTE IMMEDIATE
9.Use parameters in EXECUTE IMMEDIATE
10.Pass value out of dynamic select statement
11.Build up the CREATE TABLE statement and run it with EXECUTE IMMEDIATE
12.Use USING clause with EXECUTE IMMEDIATE to handle bind variables.
13.Use of EXECUTE IMMEDIATE for single-row queries.
14.Create Dynamic Tables
15.Call a stored procedure in dynamic script
16.how to pass a NULL value to EXECUTE IMMEDIATE.
17.Use native dynamic SQL to re-create MyTable.
18.Use DBMS_SQL to re-create MyTable.
19.execute immediate using variable
20.Create dynamic sql statement and get the result
21.Dynamically create packages
22.Use execute immediate to create dynamic query in a procedure
23.Performance between using statement and string concatenation for dynamic sql