【问题标题】:Updating multiple columns of a table更新表的多个列
【发布时间】:2009-08-05 10:12:41
【问题描述】:

我有两个包含以下字段的表:

table1:OTNAME table2 : SNCODE, description_text

我正在尝试将 table2 的两列添加到 table1 并更新这些列。我的查询是:

alter table  table1 add sncode integer                              
alter table  table1 add description_text varchar2(30)

update table1 set 
sncode,description_text = (SELECT  sncode, description_text
   FROM   table2, table1
  WHERE   SUBSTR (otname, INSTR (otname,'.', 1, 3)
                         + 1, 
                         INSTR (otname, '.', 1, 4)
                              - INSTR (otname,'.', 1, 3)
                              - 1)
                               = sncode)

我收到一个错误:ORA 00927 - 缺少等于运算符,指向我的更新语句的第二行。感谢有人能指出我正确的方向。

问候,

新手

【问题讨论】:

    标签: sql oracle oracle10g


    【解决方案1】:
    MERGE
    INTO    table1 t1
    USING   table2 t2
    ON      (SUBSTR (otname, INSTR (otname,'.', 1, 3)
                             + 1, 
                             INSTR (otname, '.', 1, 4)
                                  - INSTR (otname,'.', 1, 3)
                                  - 1)
                                   = t2.sncode))
    WHEN MATCHED THEN
    UPDATE
    SET    t1.sncode = t2.sncode,
           t1.description_text = t2.description_text
    

    你也可以简化你的表达方式:

    MERGE
    INTO    table1 t1
    USING   table2 t2
    ON      (REGEXP_SUBSTR(otname, '[^.]+', 1, 4) = t2.sncode)
    WHEN MATCHED THEN
    UPDATE
    SET    t1.sncode = t2.sncode,
           t1.description_text = t2.description_text
    

    【讨论】:

    • 谢谢.. 但我得到一个 MISSING ON KEYWORD 错误。你的代码是针对 oracle 10g 的吗?
    • 能否请您发布您的表定义以便我检查语法?
    • 嗨,不知道你的定义到底是什么意思。我正在发布表格数据类型供您参考。表 2:sncode 整数 description_text varchar2(30) 表 1:otname varchar2(100)
    • @novice:抱歉,我没有先注意到他们。我更新了帖子,现在试试。
    • 太棒了.. 非常感谢!效果很棒!
    【解决方案2】:

    您的问题是要更新的字段周围缺少括号。试试

    update table1 set 
    ( sncode,description_text) = (SELECT  sncode, description_text
       FROM   table2, table1
      WHERE   SUBSTR (otname, INSTR (otname,'.', 1, 3)
                             + 1, 
                             INSTR (otname, '.', 1, 4)
                                  - INSTR (otname,'.', 1, 3)
                                  - 1)
                                   = sncode)
    

    【讨论】:

    • 嗨 Nagul,我看到的错误是:ORA-01427: Single-row subquery returns more than one row 还有其他建议吗?
    • 我很抱歉。我只查看了您查询中的语法错误。这个查询试图做的是更新sncode,描述所有行的描述文本,其中包含从选择查询返回的一组值。要使用现有列中的构造值更新每个列,请使用其他人建议的 update..set..from 或 merge into..using..on 语法。
    【解决方案3】:

    改用 UPDATE SET FROM 构造。 类似的东西

    update table1 
    set  sncode = t1.sncode, description_text = t1.description_text 
    from table2 as t2, table1 as t1
    where SUBSTR (otname, INSTR (otname,'.', 1, 3)
                         + 1, 
                         INSTR (otname, '.', 1, 4)
                              - INSTR (otname,'.', 1, 3)
                              - 1)
                               = sncode)
    

    【讨论】:

    • 不,是关于甲骨文的。问题中没有明确提到它,但它被标记为 oracle 和 oracle10g。
    • 我使用的是 Oracle 10g。我收到一条错误消息:“SQL 命令未正确结束”
    • 你好像在sncode后面多了一个括号?以select t1.sncode, t1.description_text from table2.. 运行命令以首先了解问题所在。进一步分解 where 子句,你最终会找到错误的地方。即尝试select substr(...) from table2等等..
    • Oracle 不支持UPDATE FROM
    【解决方案4】:

    我怀疑您不应该在 SELECT 查询中包含 table1。也许这种说法会起作用:

    UPDATE table1
       SET
           (sncode, description_text)
           =
           (
             SELECT table2.sncode, table2.description_text
               FROM table2
              WHERE SUBSTR(
                      table1.otname,
                      INSTR(table1.otname,'.', 1, 3) + 1, 
                      INSTR(table1.otname, '.', 1, 4) - INSTR (table1.otname,'.', 1, 3) - 1
                    ) = table2.sncode
           )
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2016-04-05
      相关资源
      最近更新 更多