首頁  >  文章  >  資料庫  >  导入数据时, 存在的更新, 不存在的插入

导入数据时, 存在的更新, 不存在的插入

WBOY
WBOY原創
2016-06-07 14:56:391370瀏覽

导入数据时, 存在的更新, 不存在的插入 无 CREATE?trigger?tr_bank3?ON?bank3AFTER?INSERTAS??????Update?bank1?set?bank1.fmoney=bank1.fmoney+c.a???????From?????????(select?fid,sum(case?when?TYPE=0?then?fmoney?when?type=1?then?-fmoney?end)?as?a?Fr

导入数据时, 存在的更新, 不存在的插入
CREATE?trigger?tr_bank3?ON?bank3
AFTER?INSERT
AS
?
?
????Update?bank1?set?bank1.fmoney=bank1.fmoney+c.a?
??????From?
????????(select?fid,sum(case?when?TYPE=0?then?fmoney?when?type=1?then?-fmoney?end)?as?a?From?bank3?group?by?Fid)?C?
????where?
????????C.FID=bank1.FID?
????????AND?EXISTS(SELECT?1?FROM?INSERTED?WHERE?Fid=bank3.Fid))
?
????insert?into?bank1?
????select?
????????fid,
????????sum(case?when?TYPE=0?then?fmoney???when?type=1?then?-fmoney?end)?as?a?
????from?
????????bank3?AS?a
????WHERE?
????????EXISTS(SELECT?1?FROM?INSERTED?WHERE?Fid=a.Fid)
????AND?
????????NOT?EXISTS(SELECT?1?FROM?bank1?WHERE?Fid=a.Fid)
????group?by?Fid
MERGE [target] t
using [source] s on t.id = s.id
when matched then update t.name = s.name, t.age = s.age -- use "rowset1"
when not matched then insert values(id,name,age) -- use "rowset2"
when source not matched then delete; -- use "rowset3"
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn