【问题标题】:handle special characters in PL/SQL procedures.?处理 PL/SQL 过程中的特殊字符。?
【发布时间】:2014-05-13 13:14:41
【问题描述】:

我正在使用 oracle 10g plsql 程序进行插入和列出,但如果我们有任何特殊字符,如 '(单引号)和 & 等,查询将失败。如何处理plsql中的特殊字符?

之前:

    lquery := 'select count(id) into lCount
                        From
                            dual
                        where
                            name = '||iName||'
                         and Id= '||iId

之后:

     select into lCount
                        From
                            dual
                        where
                            Id= iId
                         and name = iName;

更改查询后,它的工作正常。问题是,如果我们在单引号内保留变量(如名称值),有时查询不会在更改查询后对' , " 等特殊字符执行,它的工作正常。

【问题讨论】:

  • 请编辑您的帖子以包含一个失败的代码示例。还包括有关您如何调用此代码的信息,因为这可能会影响答案。谢谢。
  • 你不应该像这样连接你的查询。改用参数select ... where id = :1 and name = :2

标签: sql plsql oracle10g


【解决方案1】:

首先如何处理引用'和&符号&

SQL@xe> set define off
SQL@xe> select q'(foo's & bar's)' from dual;

Q'(FOO'S&BAR'
-------------
foo's & bar's

SQL@xe>

另请参阅How do I ignore ampersands in a SQL script running from SQL Plus?Text Literals,了解替代引用机制q'' 的详细信息。

其次,不要将 SQL 语句创建为字符串,而是使用 PL/SQL Static SQL。静态 SQL 将自动为您处理引用(并且也是 SQL 注入安全的)。喜欢:

declare
  lCount number;
  iName varchar2(20) := q'(foo's & bar's)';
  iId number := 42;
begin
  select count(*) into lCount From dual where name = iName and Id= iId;
end;

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2018-09-24
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多