我可以在同一个查询中进行插入和更新吗?
之前:
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
是否可以在一个查询中实现这一点(不使用存储过程或触发器等)
谢谢你。