Update based on another table result

2019-09-05 21:34发布

问题:

I want to update a temp table based on another table result set.

Any suggestions. The select query works independently. But I am thinking to integrate with the update statement.

UPDATE #person_membership_promo_ext 
SET note_about=
(
    select note_text
    FROM note nt 
    INNER JOIN #person_membership_promo_ext per
      ON per.person_id=nt.main_ref_id
        and per.membership_type='P'
        and note_id=(select MAX(note_id)from note nt_1
    where nt_1.main_ref_id=per.person_id)
) 

回答1:

UPDATE per
SET note_about=nt.note_text
FROM note nt 
INNER JOIN #person_membership_promo_ext per
  ON per.person_id=nt.main_ref_id
    and per.membership_type='P'
    and note_id=(select MAX(note_id)from note nt_1
where nt_1.main_ref_id=per.person_id)


回答2:

You can use joins in update statements

UPDATE per
SET note_about = nt.note_text
FROM #person_membership_promo_ext per
INNER JOIN note nt
  ON per.person_id=nt.main_ref_id
    and per.membership_type='P'
    and note_id = (
        select MAX(note_id) 
        from note nt_1
        where nt_1.main_ref_id = per.person_id
   )


回答3:

Here you go,

Update per
set per.note_about = nt.note_text
FROM note nt 
INNER JOIN #person_membership_promo_ext per
ON per.person_id=nt.main_ref_id
and per.membership_type='P'
and note_id=(select MAX(note_id)from note nt_1
where nt_1.main_ref_id=per.person_id)

I hope this will help!