Update with case

  • Hi there,

    I need some help here, nothing happens when I execute the following,

    UPDATE A

       SET A.col_1 = CASE WHEN NOT EXISTS

    (SELECT * 

     FROM         A INNER JOIN

                          B_view ON A.ID_1 = B_view.ID_1 AND

                   A.ID_2 = B_view.ID_2)

     THEN 'N' ELSE 'Y' END;

    When I run it, it comes up with a (1234564 row(s) affected), and when I look in table A nothing has happend?

    I must be missing something here, and I don't know what it is.

    Regards joejoe

  • Well actually everything gets updated to 'N'.

    You don't need the exists clause here, just use a left join to make this update >>

    UPDATE A Set A.col_1 = CASE WHEN B.col_1 IS NULL THEN 'N' ELSE 'Y' END FROM dbo.A A LEFT OUTER JOIN dbo.ViewA B ON A.id = B.id

  • Thanks RGR'us

    But when I run your script, the same thing happens

    (1234564 row(s) affected)

    But nothing is changed, it's all NULLs ???

     

     

     

     

  • The probelm is solved, allways remember to see if there is triggers on table

    Regards Joejoe 

  • Yup .

Viewing 5 posts - 1 through 4 (of 4 total)

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