更新表的多个列

时间:2009-08-05 10:12:42

标签: sql oracle oracle10g

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

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 - 缺少等于运算符,指向更新语句的第二行。感谢有人能指出我正确的方向。

此致

新手

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

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

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)

答案 2 :(得分:0)

尝试使用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)

答案 3 :(得分:0)

我怀疑您不应该在table1查询中加入SELECT。也许这句话可行:

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
       )