如何逃避“:”在Oracle动态SQL中也有绑定变量?
我正在尝试将以下内容设为动态 SQL,但是 :
字符弄乱了 -
alter session set events 'sql_trace [sql: asasasaass]';
示例:
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;
/
以上失败并显示:
ERROR at line 1:
ORA-01006: bind variable does not exist
根据 SQL 语法,需要一个 :
,另一个 :
用于绑定变量。
除了连接绑定值之外,还有关于如何解决此问题的任何想法吗?
-- 4 月 4 日下午 5 点 10 分(CST)编辑添加以下内容:
Alter session 不是 DDL 命令。下面是证明。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于这个语句,我只是忘记使用绑定变量,例如:
For this statement I'd just forget about using a bind variable, e.g.:
您不能将绑定变量与 DDL 一起使用:
此外,您不会收到此有意义的错误消息,因为
:
字符已在语句中转义,因为它们位于引号之间 ('< /代码>)。
You can't use bind variables with DDL:
In addition, you don't get this meaningful error message because the
:
characters are already escaped in your statement since they are between quotes ('
).不能将绑定变量与 DDL 一起使用。对于 PL/SQL 中的 DML,您也不能使用绑定变量,因为当您将值连接到 SQL 语句时它们会自动应用。对 PL/SQL 变量的每个引用实际上都是一个绑定变量。
http://www.akadia.com/services/ora_bind_variables.html
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
我还得到了以下解释,与上述答案相关:
I was also given following explanation, which correlate with above answers: