如何使用%字符串比较编写动态查询?
我在PL/SQL过程中编写动态查询,其中我想根据字符串比较操作检索记录。我的字符串存储在一个变量中,我想在查询中使用它。
x:='A';
select * from table_name where category like 'A%SITE';
我想以某种方式将上述内容转换为动态查询,并将变量x替换为“ a%站点”中的x,我不确定如何处理。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用字符串串联:
db<> fiddle
关于您的评论:
您需要通过将报价加倍来逃避报价,并且您可能还想使用绑定变量(以防止SQL注入攻击):
但是,该查询中没有任何需要动态SQL的问题,并且您无需动态SQL即可完全相同。如果您要么使用光标(按照我的示例)或使用
选择... [Bulk Collect]
中。Use string concatenation:
db<>fiddle here
Regarding you comment:
You need to escape the quotes by doubling them up and you probably also want to use a bind variable (to prevent SQL injection attacks):
However, there is nothing in that query that requires dynamic SQL and you can do exactly the same without dynamic SQL if you either use a cursor (as per my example above) or use
SELECT ... [BULK COLLECT] INTO
.