0
我可以在同一查詢中執行插入和更新嗎?在同一查詢中使用UPDATE和INSERT
前:
MemberID | SubsID | StartDate | EndDate
------------------------------------------------
1001 | 10 | 2012-12-21 | 2012-12-31
2002 | 10 | 2012-12-22 |
後:
MemberID | SubsID | StartDate | EndDate
------------------------------------------------
1001 | 10 | 2012-12-21 | 2012-12-31
2002 | 10 | 2012-12-22 | 2012-04-13
2002 | 10 | 2012-04-13 |
獲取行:
select * from MemberSubs
where SubsID = 10 and EndDate is null;
個插入新行:
insert into
MemberSubs(MemberID, SubsID, Price, StartDate)
select
MemberID, SubsID, Price, Current Date
from
MemberSubs
where
SubsID = 10
and
EndDate is null
更新的舊行:
update MemberSubs
set
EndDate = current date
where
SubsID = 10
and
EndDate is null
and
StartDate < Current Date
是否有可能在一個查詢做到這一點(不使用存儲過程或觸發等)
謝謝您。
重複如上 - 你正在尋找'MERGE'語句。 – Yuck
感謝關於MERGE語句的主題,但是有人可以提供關於如何使用MERGE實現此目的的提示嗎?我的MERGE查詢將如何?我從來沒有用過MERGE。 – MrSimpleMind
我不明白MERGE如何解決這個問題?你確定這可以通過使用MERGE語句來解決嗎?我已經閱讀了ibm手冊和其他pdf等我不知道如何做更新和插入相同的查詢,它不是「更新或插入」它是「更新和插入」(或「插入和更新」無論我從哪開始)。 – MrSimpleMind