-1

I have the following two tables

activity

enter image description here

activity_bak

enter image description here

I want to take the comments from activity_bak and update the comments in activity to match by using the corresponding activity_no and activity_seq.

I've tried doing it like this but to no success:

update Animal.sysadm.activity
       set activity_comment = ab.activity_comment
           from Animal.SYSADM.activity a
               left join Animal.SYSADM.activity_bak ab
          on ab.activity_no = a.activity_no
               left join Animal.sysadm.activity_bak ab2
          on ab2.activity_seq = a.activity_seq

Any help or pointers would be greatly appreciated.

sunkuet02
  • 2,376
  • 1
  • 26
  • 33
ghoston3rd
  • 129
  • 2
  • 5
  • 14
  • Possible duplicate of [How can I do an UPDATE statement with JOIN in SQL?](http://stackoverflow.com/questions/1293330/how-can-i-do-an-update-statement-with-join-in-sql) – Tab Alleman May 17 '16 at 17:56

2 Answers2

-1

No need to do 2 joins, you need just one. The right syntax is:

UPDATE a
SET a.activity_comment = ab.activity_comment
FROM Animal.SYSADM.activity a
INNER JOIN Animal.SYSADM.activity_bak ab
    ON ab.activity_no = a.activity_no
    AND ab.activity_seq = a.activity_seq;
Lamak
  • 69,480
  • 12
  • 108
  • 116
-1

I think you want:

update A set activity_comment = ab.activity_comment
    from Animal.SYSADM.activity a
    left join Animal.SYSADM.activity_bak ab
        on ab.activity_no = a.activity_no
        And ab.activity_seq = a.activity_seq
Joe C
  • 3,925
  • 2
  • 11
  • 31