Updating a field based on the value of another field

  • Hi all,

    I am a newbie to SQL server and at the moment I am trying to write a trigger that fires if the value of a field is changed. The trigger works i.e. I can update the filed to a value I specify

    eg UPDATE Employees SET lastname = 'papa' WHERE lastname = 'king'

    What I would like to do is update the field to a value stored in another field in the database. Is this possible and how would I go about doing it.

    Something like this

    UPDATE Employees SET lastname = ?????? WHERE lastname = 'king'

    Where the ????? would be a reference to the field that contains the value

    Regards

    Lee

  • If you want to update the lastname value to equal the value of a column that has the lastname value of king

     

    update Employees SET lastname = newlastname where lastname = 'king' --(newlastname being the column that you want to use to populate the lastname column)



    Shamless self promotion - read my blog http://sirsql.net

  • Unless that field isn't in the same table then you do something like this.

     

    Update E

    SET Lastname = T2.newlast

    FROM

    Employees E

    INNER JOIN

    table2 T2

    ON

    E.coltolink = T2.coltolink

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

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