Posted on 2009-05-06 15:52
Prayer 閱讀(489)
評論(0) 編輯 收藏 引用 所屬分類:
DB2
關(guān)聯(lián)表更新sybase SqlServer和MS SqlServer的語法比較簡單實用,相比DB2和Oracle就比較tu一點,而且要注意在更新的范圍一定要有限制,否則關(guān)聯(lián)之外的字段都會被更新為[null]:
UPDATE tab1 a
SET a.col1 = (select b.col1 from tab2 b where a.col2 = b.col2)
WHERE a.col2 IN (SELECT b.col2 FROM tab2 )
還有在子查詢中的結(jié)果集對于必須是唯一的!
update tab1 a
set a.col1 = (select b.col1 from tab2 b where a.col2 = b.col2)
where ...
(select b.col1 from tab2 b where a.col2 = b.col2) 必須唯一。