转义 SQL 中的特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8764370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:53:55  来源:igfitidea点击:

Escaping special characters in SQL

sqloracle11g

提问by contactmatt

Is there an easy way in Oracle to escape special characters in a SQL statement? (i.e. %, &, ') I saw this linkin regard to manually escaping characters, but I thought Oracle may have provided an easier way to do so.

Oracle 中是否有一种简单的方法来转义 SQL 语句中的特殊字符?(即 %, &, ')我看到了有关手动转义字符的链接,但我认为 Oracle 可能提供了一种更简单的方法。

Note: I'm generating dynamic SQL select statements through an ORM.

注意:我正在通过 ORM 生成动态 SQL 选择语句。

回答by Adam Musch

If using bind variables and ORM, embedded single quotes and ampersands should be handed automatically; those are special characters in SQL*Plus or SQL*Developer.

如果使用绑定变量和 ORM,应该自动传递嵌入的单引号和与号;这些是 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 escapeclause of the likecondition:

要使用 LIKE 查找文字字符 % 和 _ (不是它们的多字符和单字符通配符版本),您可以使用条件的escape子句like

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

will only return the rows where some_text begins with a percent sign.

只会返回 some_text 以百分号开头的行。

回答by Eduardo

It seems you're looking for something like the command SET DEFINE OFF, which you can run and it affects the whole SQL session. This command, however, only prevents Oracle from giving special meaning to the ampersand character. It doesn't affect other special characters such as the single quote.

看来您正在寻找类似于命令 SET DEFINE OFF 的东西,您可以运行它并且它会影响整个 SQL 会话。但是,此命令只会阻止 Oracle 赋予与号字符特殊含义。它不会影响其他特殊字符,例如单引号。

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

回答by guzzibill

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