简体   繁体   中英

Update based on another table result

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)
) 
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)

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
   )

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM