views:

247

answers:

4

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

A: 

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)
Ray
No, it is about Oracle. It's not explicitly mentioned in the question, but it is tagged with oracle and oracle10g.
nagul
I'm using Oracle 10g. I get an error saying : " SQL COMMAND NOT PROPERLY ENDED "
novice
You seem to have an extra paranthesis after sncode? Run the command as `select t1.sncode, t1.description_text from table2..` to first understand what's wrong. Further break down the where clause and you'll eventually get to the bit that's wrong. i.e Try `select substr(...) from table2` and so on ..
nagul
`Oracle` does not support `UPDATE FROM`
Quassnoi
+1  A: 
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
Quassnoi
Thanks.. but I get a MISSING ON KEYWORD error. Is your code targetted to oracle 10g ?
novice
Could you please post your table definitions so that I could check the syntax?
Quassnoi
See the post update
Quassnoi
Hi, Not sure what exactly you meant by definitions. I'm posting the tables data types for your reference.Table 2: sncode integerdescription_text varchar2(30)Table 1: otname varchar2(100)
novice
@novice: sorry, I didn't notice them first. I updated the post, try now.
Quassnoi
Fantastic.. Thanks a ton ! Works fabulously!
novice
Hello Quassnoi,Can you briefly tell me how this line works :(REGEXP_SUBSTR(otname, '[^.]+', 1, 4) = t2.sncode)thanks!
novice
It's a regular expression. It searches the string for the `4th` sequence consisting of everything but the periods.
Quassnoi
`.` is "a period", `[^.]` is "everything but a period", `[^.]+` is "a longest possible sequence of everything that is not a period", `REGEXP_SUBSTR(otname, '[^.]+', 1, 4)` is "the `4th` longest possible sequence of everything that is not a period, starting from symbol `1`"
Quassnoi
Thanks Quassnoi for the detailed explanation.
novice
+2  A: 

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)
nagul
Hi Nagul, the error I see is :ORA-01427: Single-row subquery returns more than one row Any other suggestions ?
novice
My apologies. I looked only at the syntax error in your query. What this query is trying to do is update sncode, description_text for *all* rows with 1 set of value returned from the select query. To update each with the constructed value from existing columns, use the update..set..from or merge into..using..on syntax suggested by others.
nagul
A: 

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
       )
Adam Paynter