Using CASE to UPDATE a column

  • Hello,

    I am trying to create a CASE statment which will update a column depending on the outcome of a select statement.

    The following stored procedure works without using CASE:

    UPDATE <TABLE2>

    SET <COLUMN_X> = 'N' 

    FROM <TABLE1> RIGHT OUTER JOIN

                          <TABLE2> ON <TABLE1.COLUMNID> = <TABLE2.COLUMNID>

    WHERE     (<TABLE1.COLUMNID> IS NULL)

    I need to set up a stored procedure which will set 'N' if in the where statement is NULL and set 'Y' if the where statement is NOT NULL.

     

    Hope this makes sense.

     

    Thanks in advance

     

     

     

     

     

  • Is this what you're looking for ?

    I prefer using Left outerjoins because I find them to be more readable.

    I also like using objectaliases because I find them to make the query more readable.

    UPDATE <TABLE2>

    SET <COLUMN_X> = case when <T1.COLUMNID> IS NULL then 'N' 

                                        else 'Y'

                                        end

    FROM <TABLE2>  T2

    LEFT OUTER JOIN <TABLE1> T1

    ON <T2.COLUMNID> = <T1.COLUMNID>

    -- WHERE     (<TABLE1.COLUMNID> IS NULL)

    Test it for your means

     

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

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

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