Oracle、动态 SQL 和命名参数
(我发现这部分回答了问题,虽然考虑到我的sql将在oracle和mssql中使用,但声明方式看起来会更整洁:) 在动态PL/SQL中绑定变量
我有一些动态sql使用如下语法执行:
EXECUTE IMMEDIATE plsql_block USING employeeid, sortname;
然后我可以使用 :1、:2 等访问动态 sql 中的这些变量。 我可以使用命名参数吗?类似的东西
EXECUTE IMMEDIATE plsql_block USING employeeid => employeeid
,然后使用 :employeeid 在动态 sql 中访问它们,而不是依赖于职位?
如果不是,我的想法是在sql的开头做这样的事情:
declare employeeid varchar(15 := :1;
然后我可以将我的动态sql更改为我想要的内容,而不必担心定位。
有更好的办法吗?
谢谢
(I've found this which partly answers the question, the declare way would look neater though given that my sql is will be used in oracle and mssql:)
Binding variables in dynamic PL/SQL
I have some dynamic sql which I'm executing using syntax like the below:
EXECUTE IMMEDIATE plsql_block USING employeeid, sortname;
and I can then access those variables inside the dynamic sql using :1, :2, etc.
Can I use named parameters instead? something like
EXECUTE IMMEDIATE plsql_block USING employeeid => employeeid
and then access them inside the dynamic sql using :employeeid rather than relying on position?
If not my thought is to do something like this at the beginning of the sql:
declare employeeid varchar(15 := :1;
and then I can change my dynamic sql to my hearts content without worrying about positioning.
Is there a better way?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的解决方法是我过去使用过的。
还有很好的旧 DBMS_Sql,它允许像通过 OCI 那样绑定变量。 (这基本上就是 DBMS_Sql 似乎正在做的事情;-))
Your workaround is what I have used in the past.
There's also good old DBMS_Sql, which allows binding variables like you would be able to via OCI. (Which is basically what DBMS_Sql seems to be doing anyways ;-) )
您可以在 DBMS_SQL 中使用命名参数:
You can use named arguments in
DBMS_SQL
: