根据另一个表结果进行更新

时间:2014-01-15 23:45:07

标签: sql sql-server sql-server-2008 tsql

我想根据另一个表结果集更新临时表。

任何建议。选择查询独立工作。但我正在考虑与更新声明集成。

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

3 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

您可以在更新语句中使用联接

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
   )

答案 2 :(得分:0)

你走了,

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)

我希望这会有所帮助!