oracle中是否有任何函数可以在sql查询中转义错误的字符?我有从不同字符串构建查询的代码,其中一些可能包含 '
字符,这会破坏 sql 查询。
最佳答案
正如 Yahia 所指出的,您应该始终使用绑定(bind)变量,而不是动态地动态组装 SQL 语句。这是保护自己免受 SQL 注入(inject)攻击的正确方法。转义字符串提供了低得多的保护级别。
话虽如此,假设您使用的是 Oracle 10.1 或更高版本,您可以使用 q 引用语法。就像是
1 select q'[This is a string with an embedded ']' str
2* from dual
SQL> /
STR
-----------------------------------
This is a string with an embedded '
您可以将 [ 和 ] 字符替换为许多其他字符,具体取决于字符串中可能出现的字符
1 select q'<This is a string with an embedded '>' str
2* from dual
SQL> /
STR
-----------------------------------
This is a string with an embedded '
SQL> ed
Wrote file afiedt.buf
1 select q'{This is a string with an embedded '}' str
2* from dual
SQL> /
STR
-----------------------------------
This is a string with an embedded '
关于oracle转义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133317/