I'm trying to make the following a dynamic SQL, but :
character is messing up -
alter session set events 'sql_trace [sql: asasasaass]';
Example:
declare
l_trc_cmd varchar2(500);
l_sql_id varchar2(500) := 'asasasaass';
begin
l_trc_cmd := q'# alter session set events 'sql_trace [sql: :L_SQL_ID]' #';
execute immediate l_trc_cmd using l_sql_id;
end;
/
Above fails with:
ERROR at line 1:
ORA-01006: bind variable does not exist
One :
is required as per syntax of the SQL, and another :
is for bind variable.
Any ideas on how to fix this other than concatenating the bind value?
-- Edited on April 4th at 5.10pm CST to add following:
Alter session is not DDL command. Below is proof.
sqlplus+> select * from t2;
A
----------
1
1 row selected.
sqlplus+> insert into t2 values(2);
1 row created.
sqlplus+> alter session set tracefile_identifier ="umappsperf1" statistics_level=all;
Session altered.
sqlplus+> alter session set events 'sql_trace wait=true';
Session altered.
sqlplus+> select * from t2;
A
----------
2
1
2 rows selected.
sqlplus+> rollback;
Rollback complete.
sqlplus+> select * from t2;
A
----------
1
1 row selected.
Rules for using bind variables with Execute Immediate of Native Dynamic SQL. In native dynamic SQL we need to list down the values for all the bind variables used in the SQL query beforehand. You cannot use schema object names such as table name as bind argument in native dynamic SQL.
Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.
Bind parameters—also called dynamic parameters or bind variables—are an alternative way to pass data to the database. Instead of putting the values directly into the SQL statement, you just use a placeholder like ? , :name or @name and provide the actual values using a separate API call.
For this statement I'd just forget about using a bind variable, e.g.:
declare
l_trc_cmd varchar2(500);
l_sql_id varchar2(500) := 'asasasaass';
begin
l_trc_cmd := REPLACE(
q'# alter session set events 'sql_trace [sql: %SQLID%]' #'
,'%SQLID%',l_sql_id);
execute immediate l_trc_cmd;
end;
/
You can't use bind variables with DDL:
SQL> exec execute immediate 'CREATE TABLE test AS SELECT :x t FROM DUAL' USING 1;
ORA-01027: bind variables not allowed for data definition operations
ORA-06512: at line 2
In addition, you don't get this meaningful error message because the :
characters are already escaped in your statement since they are between quotes ('
).
You cannot use bind variables with DDL. With DML in PL/SQL, you cannot use bind variables either because they are automatically applied when you concatenate values to SQL statements. Each reference to a PL/SQL variable is in fact a bind variable.
http://www.akadia.com/services/ora_bind_variables.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With