dcsimg
Results 1 to 2 of 2

Thread: Using Case and Joins in update statement

  1. #1
    Join Date
    Aug 2011
    Posts
    1

    Smile Using Case and Joins in update statement

    Hello everyone..!

    I need to update a table by using both Join and case statements. I am not very good at SQL. Please find the code given below written by me...
    update t1 a
    set a.name2=
    (case
    when b.msg2 in ('bingo') then '1'
    when b.msg2 in ('andrew') then '2'
    when b.msg2 in ('sam') then '3'
    else '4'
    end )
    from t1 a left outer join t2 b
    on a.name1 = b.msg1 ;

    I am getting an error saying the SQL command not ended properly..Please help me...

    I really appreciate your help !
    Thanks in advance...

  2. #2
    Join Date
    Feb 2012
    Posts
    3
    Hi Buddy
    I am also new to sql

    But why do you need to use join in this particular case.
    You can probably achieve the results by using the below code

    UPDATE t1 a
    SET t1.name2 =
    (CASE
    WHEN b.msg2 = 'bingo' THEN '1'
    WHEN b.msg2 = 'andrew' THEN '2'
    WHEN b.msg2 = 'sam' THEN '3'
    ELSE '4'
    END
    FROM t2 b
    WHERE a.name1=b.msg1);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •