2009-08-05 67 views
0

我有两个表具有以下字段:更新表的多个列

表1:OTNAME 表2:SNCODE,描述文本

我想表2的两列添加到表1和更新列。我的查询是:

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
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 
0

尝试更新设置为从结构代替。 喜欢的东西

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) 
+0

不,这是关于甲骨文。这个问题没有明确提到,但它是用oracle和oracle10g标记的。 – nagul 2009-08-05 10:25:22

+0

我正在使用Oracle 10g。我得到一个错误:“SQL命令没有正确结束” – novice 2009-08-05 10:26:36

+0

sncode之后似乎有一个额外的paranthesis?运行该命令作为`select t1.sncode,t1.description_text from table2..`来首先了解什么是错误的。进一步细分where子句,最终你会发现错误的位。即尝试`从table2`选择substr(...)等等。 – nagul 2009-08-05 11:19:51

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) 
0

我怀疑你不应该包括在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 
     )