我正在尝试防止预订系统(C# 到 SQL Server)中的重复预订。
我正在使用存储过程并尝试了以下操作:
alter proc Datumkoll
@DatumFrom smalldatetime = null,
@datumTill smalldatetime = null
as
select RumsNummer
from rum
where
RumsNummer not in (select rumsnummer
from rumsbokning
where datumfrån >= @datumfrom and datumtill <= @datumTill)
GO
如果日期是在现有预订中设置的,则此代码不会阻止重复预订
最佳答案
想象一下时间窗口和选项:
不重叠:
[new]
[existing]
部分重叠:
[new] [new]
[existing] [existing]
遏制:
[new] [-------new---------]
[existing] [existing]
您的支票仅处理最后一个;你需要回去考虑其他人。诀窍在于实际上只需要 3 项测试:
- 新的开始日期位于现有预订中
- 新的结束日期位于现有预订中
- 现有预订完全包含在新预订中
这给了我们:
where (datumfrån>=@datumfrom and datumtill <= @datumTill)
or (@datumfrom >= datumfrån and @datumfrom <= datumtill)
or (@datumTill >= datumfrån and @datumfrom <= datumtill)
(另请参阅替代结构的评论)
关于c# - 防止 SQL 重复预订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395573/