【问题标题】:Use special character '%' to append the string in update statement使用特殊字符 '%' 在更新语句中附加字符串
【发布时间】:2017-07-12 02:54:36
【问题描述】:

我需要更新表列为varchar2 数据类型的表,并且需要更新具有'%' 的列的值。

例如——

create table test (id number, name varchar2(20));

insert into test values (1, 'smith');
insert into test values (2, 'allen');

现在我们需要将 NAME 列中的值更新为 smith'%'

所以它还应该在字符串中包含单引号。

我可以将其更新为 smith%,但应该是 smith'%'

update test
  set name = 'smith'||'''%'''
where id = 1;

SQL 错误:ORA-00911:无效字符

【问题讨论】:

  • 更新不应失败 AFAIK。您确定这是您运行的实际查询吗?
  • 我认为 '%' 不是那个字符 ||是
  • 它对我有用。 @TimBiegeleisen 是对的。你确定你运行的查询正常吗?
  • @pablomatico 也许 Oracle 有一些无法使用 || 进行连接的模式?
  • 嗯,也许......你在哪里运行更新语句?

标签: sql oracle sql-update


【解决方案1】:

您的查询在 SQLPlus 中完美运行:

SQL> update test
  2    set name = 'smith'||'''%'''
  3  where id = 1;

1 row updated.

SQL> select * from test;

        ID NAME
---------- --------------------
         1 smith'%'
         2 allen

这可能是另一种方式,避免需要双引号:

SQL> update test
  2    set name = 'allen'|| q'['%']'
  3  where id = 2;

1 row updated.

SQL> select * from test;

        ID NAME
---------- --------------------
         1 smith'%'
         2 allen'%'

甚至,避免||

SQL> update test
  2    set name = concat(name, q'['%']')
  3  where id = 1;

1 row updated.

SQL> select * from test;

        ID NAME
---------- --------------------
         1 smith'%''%'
         2 allen'%'

【讨论】:

    【解决方案2】:

    SQL> 设置定义关闭;

    验证此链接

    how-to-enter-special-characters-like-in-oracle-database

    【讨论】:

    • 这不会影响 % 的处理方式。 OP 的问题中没有和号。
    • 所有都属于特殊字符
    • set define off% 符号无关。
    【解决方案3】:

    其实你的语句应该可以的,我只是在我的数据库中测试过。

    update test
      set name = concat('smith', '''%''')
    where id = 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多