简体   繁体   English

转义SQL中的特殊字符

[英]Escaping special characters in SQL

Is there an easy way in Oracle to escape special characters in a SQL statement? Oracle中有一种简单的方法可以在SQL语句中转义特殊字符吗? (ie %, &, ') I saw this link in regard to manually escaping characters, but I thought Oracle may have provided an easier way to do so. (即%,&,')我看到了关于手动转义字符的这个链接 ,但我认为Oracle可能提供了一种更简单的方法。

Note: I'm generating dynamic SQL select statements through an ORM. 注意:我正在通过ORM生成动态SQL select语句。

If using bind variables and ORM, embedded single quotes and ampersands should be handed automatically; 如果使用绑定变量和ORM,则应自动传递嵌入的单引号和&符号; those are special characters in SQL*Plus or SQL*Developer. 这些是SQL * Plus或SQL * Developer中的特殊字符。

To use LIKE where looking for the literal characters % and _ (not their multi- and single-character wildcard versions), you'd use the escape clause of the like condition: 要在查找文字字符%和_(不是它们的多字符和单字符通配符版本)时使用LIKE,您可以使用like条件的escape子句:

select * from my_table where some_text like '/%%' escape '/';

will only return the rows where some_text begins with a percent sign. 只返回some_text以百分号开头的行。

It seems you're looking for something like the command SET DEFINE OFF, which you can run and it affects the whole SQL session. 看起来你正在寻找类似命令SET DEFINE OFF的东西,你可以运行它,它会影响整个SQL会话。 This command, however, only prevents Oracle from giving special meaning to the ampersand character. 但是,此命令仅阻止Oracle赋予&符号特殊含义。 It doesn't affect other special characters such as the single quote. 它不会影响其他特殊字符,例如单引号。

A couple of links to additional information regarding escaping characters follow: 有关转义字符的其他信息的几个链接如下:

https://forums.oracle.com/forums/thread.jspa?threadID=2256637 https://forums.oracle.com/forums/thread.jspa?threadID=2256637

http://docs.oracle.com/cd/B10501_01/text.920/a96518/cqspcl.htm http://docs.oracle.com/cd/B10501_01/text.920/a96518/cqspcl.htm

here's the definitive answer page on tech on the net. 这是关于网络技术的最终答案页面。 Even provides examples and exercises 甚至提供示例和练习

http://www.techonthenet.com/sql/like.php http://www.techonthenet.com/sql/like.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM