novice
novice

Reputation: 165

Updating multiple columns of a table

I've two tables with the following fields:

table1 : OTNAME table2 : SNCODE, description_text

I'm trying to add the two columns of table2 to table1 and update the columns. My query is:

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)

I get an error: ORA 00927 - Missing Equal to Operator, pointing to the second line of my update statement. Appreciate if someone can point me in the right direction.

Regards,

novice

Upvotes: 0

Views: 6917

Answers (4)

Adam Paynter
Adam Paynter

Reputation: 46938

I suspect you should not be including table1 in the SELECT query. Perhaps this statement will work:

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
       )

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425803

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

You also can simplify your expression:

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

Upvotes: 5

nagul
nagul

Reputation: 2263

Your problem is you are missing brackets around the fields to be updated. Try

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)

Upvotes: 1

Ray
Ray

Reputation: 1585

Try UPDATE SET FROM construct instead. Something like

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)

Upvotes: 0

Related Questions