SQL Update + set + select question.

  • Hi everybody,

    Not sure if i´m posting in the right section here. I´m trying to get an SQL statement to work but i´m stuck .. can´t get it to work.

    I have 3 tables.

    ------

    table1

    - obj_num

    - name

    - seq

    table2 // custom

    - obj_num

    - name

    - mi_class_name

    table3

    - seq

    - mi_class_name

    ------

    obj_num and name are the same in table1, table2. I want to update the seq number in table1 from table3 WHERE table2.mi_class_name = table3.mi_class_name are the same.

    I´ve tried this statement below without any luck.

    UPDATE m.table1

    SET table1.seq = (SELECT table3.seq

    FROM m.table3

    INNER JOIN table2.citems

    WHERE citems.name = table1.name)

    WHERE EXISTS

    (SELECT table3.name

    FROM c.table3

    WHERE table3.name = table1.name);

    // Regards

  • UPDATE table1

    SET seq =

    (

    SELECT T3.seq

    FROM table3 T3

    WHERE EXISTS

    (

    SELECT *

    FROM table2 T2

    WHERE T2.mi_class_name = T3.mi_class_name

    AND T2.obj_num = table1.obj_num

    AND T2.[name] = table1.[name]

    )

    )

    WHERE EXISTS

    (

    SELECT *

    FROM table3 T31

    WHERE EXISTS

    (

    SELECT *

    FROM table2 T21

    WHERE T21.mi_class_name = T31.mi_class_name

    AND T21.obj_num = table1.obj_num

    AND T21.[name] = table1.[name]

    )

    )

  • Thx a lot Mr or Mrs. 500!

    It worked like a charm 😀

    Many thx

    // Regards

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply