[英]Sybase SQL update with self-join
Sybase SQL中使用自連接進行更新的正確語法是什么? 例如,假設您具有下表(#tmptbl):
account | client |amount | date
-------------------------------------
ACT1 | CLIENTA | 12 |2010-12-30
ACT2 | CLIENTB | 5 |2010-12-30
ACT1 | CLIENTA | 17 |2010-12-31
ACT2 | CLIENTB | 6 |2010-12-31
我想用2010-12-30的金額值覆蓋2010-12-31的金額。
我感覺像這樣寫:
update old
set old.amount = new.amount
from #tmptbl old, #tmptbl new
where
/*filter old*/
old.account = 'ACT1'
and old.date = '2010-12-30'
and old.client = 'CLIENTA'
/* self-join new and old*/
and old.account = new.account
and old.client = new.client
/* filter new */
and new.date = '2010-12-31'
但是看起來Sybase在'update <>'子句中似乎不接受別名。 這樣做的正確方法是什么?
謝謝!
這有效:
update #tmptbl
set old.amount = new.amount
from #tmptbl old, #tmptbl new
where
/*filter old*/
old.account = 'ACT1'
and old.date = '2010-12-30'
and old.client = 'CLIENTA'
/* self-join new and old*/
and old.account = new.account
and old.client = new.client
/* filter new */
and new.date = '2010-12-31'
go
如果省略了要更新的表的別名,即set amount = new.amount
則Sybase會將要更新的表與from子句中的第一個匹配表相關聯,因此在這種情況下, set amount = new.amount
更新set amount = new.amount
需要from來讀取from #tmptbl new, #tmptbl old
。
輸出:
account client amount date
---------- --------- --------- ----------------
ACT1 CLIENTA 12 30/12/2010 00:00
ACT2 CLIENTB 5 30/12/2010 00:00
ACT2 CLIENTB 6 31/12/2010 00:00
ACT1 CLIENTA 12 31/12/2010 00:00
你有沒有嘗試過
update #tmptbl
set amount = new.amount
from #tmptbl old, #tmptbl new
where
/*filter old*/
old.account = 'ACT1'
and old.date = '2010-12-30'
and old.client = 'CLIENTA'
/* self-join new and old*/
and old.account = new.account
and old.client = new.client
/* filter new */
and new.date = '2010-12-31'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.