Using CASE with Update

  • Hello folks,

    I'm having a heck of a time figuring the following problem:

    In my database I have two tables (tbl1 and tbl2). I would like to update feild [ln] in tbl1 with data from feild G1FirstName in tbl2. When I use the following query I get the following error:

    Server: Msg 207, Level 16, State 3, Line 1

    Invalid column name 'G1FirstName'.

    Server: Msg 207, Level 16, State 1, Line 1

    Invalid column name 'G1FirstName'.

    UPDATE dbo..tbl1.stu

    SET dbo.tbl1.ln =

    CASE

    WHEN dbo.tbl2.G1FirstName <> ''

    THEN dbo.tbl2.G1FirstName

    ELSE Null

    END

    Your help is greatly appreciated.

    Thank you.

  • >>UPDATE dbo..tbl1.stu

    What is "dbo..tbl1.stu" ?

    The UPDATE statement requires the name of a table, or a table alias. In 3 part naming that would be DBName.dbo.TableName.

    Then you need something to join the rows in tbl2 to tbl1. What is the key column or columns that link the 2 tables ?

     

  • table name questions aside, I think the update stmt with a case would go something like this:

    update tbl1

    set tbl1.ln = CASE tbl2.G1FirstName

           WHEN <> '' then tbl2.G1FirstName

           ELSE Null

           END

    from tbl1

    inner join tbl2 on

          tbl1.<somekey> = tbl2.<somekey>


    I keep a close watch on this data of mine, I keep a connection to the database open all the time...

    Gianni Cache

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

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