Sql 更新表的多个列

Sql 更新表的多个列,sql,oracle,oracle10g,Sql,Oracle,Oracle10g,我有两个具有以下字段的表: 表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 sn

我有两个具有以下字段的表:

表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-缺少等于运算符,指向update语句的第二行。如果有人能给我指出正确的方向,我将不胜感激

问候,


新手

尝试从构造更新集。 差不多

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)

请尝试从构造更新集。 差不多

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)
您还可以简化表达式:

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
您还可以简化表达式:

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

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

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)

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

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)

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

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


不,是关于甲骨文的。问题中没有明确提到它,但它被标记为oracle和oracle10g。我使用的是oracle10g。我收到一个错误:“SQL命令未正确结束”您似乎在sncode之后有额外的妄想?以
的方式运行命令,从表2中选择t1.sncode,t1.description\u text..
首先了解问题所在。进一步分解where子句,最终会找到错误的地方。i、 e尝试从表2中选择substr(…),依此类推。
Oracle
不支持
updatefrom
否,它是关于Oracle的。问题中没有明确提到它,但它被标记为oracle和oracle10g。我使用的是oracle10g。我收到一个错误:“SQL命令未正确结束”您似乎在sncode之后有额外的妄想?以
的方式运行命令,从表2中选择t1.sncode,t1.description\u text..
首先了解问题所在。进一步分解where子句,最终会找到错误的地方。i、 e请尝试从表2中选择substr(…),依此类推。
Oracle
不支持
updatefrom
谢谢。。但是我得到了一个缺少关键字的错误。您的代码是针对oracle 10g的吗?请发布您的表定义,以便我检查语法。您好,不确定定义的确切含义。我正在发布表格数据类型供您参考。表2:sncode integer description_text varchar2(30)表1:otname varchar2(100)@新手:对不起,我没有先注意到它们。我更新了帖子,现在试试。太棒了。。非常感谢!工作非常出色!谢谢但是我得到了一个缺少关键字的错误。您的代码是针对oracle 10g的吗?请发布您的表定义,以便我检查语法。您好,不确定定义的确切含义。我正在发布表格数据类型供您参考。表2:sncode integer description_text varchar2(30)表1:otname varchar2(100)@新手:对不起,我没有先注意到它们。我更新了帖子,现在试试。太棒了。。非常感谢!工作非常出色!嗨,Nagul,我看到的错误是:ORA-01427:单行子查询返回多行。还有其他建议吗?我很抱歉。我只查看了查询中的语法错误。此查询试图做的是使用select查询返回的1组值更新所有行的sncode、description\u文本。要使用现有列中的构造值更新每个列,请在其他人建议的语法上使用update..set..from或merge into..using..on。您好,Nagul,我看到的错误是:ORA-01427:单行子查询返回多行任何其他建议?抱歉。我只查看了查询中的语法错误。此查询试图做的是使用select查询返回的1组值更新所有行的sncode、description\u文本。要使用现有列中构造的值更新每个列,请根据其他人建议的语法使用update..set..from或merge into..using。