我坚持这个问题,我想阻止双重预订的发生。这是我一直在使用的代码:
USE INL5
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_bookinginfo_doublebooking] ON [dbo].[bookinginfo]
FOR INSERT AS
DECLARE @startdate AS DATE
DECLARE @enddate AS DATE
DECLARE @roomnumber AS CHAR(3)
SELECT @startdate = inserted.startdate, @enddate = inserted.enddate, @roomnumber = inserted.roomnumber
FROM inserted, bookinginfo
WHERE @roomnumber = bookinginfo.roomnumber AND (@startdate BETWEEN bookinginfo.startdate AND bookinginfo.enddate) AND (@enddate BETWEEN bookinginfo.startdate AND bookinginfo.enddate)
IF EXISTS(SELECT * FROM inserted)
BEGIN RAISERROR ('Double bookings are not allowed',16,1)
ROLLBACK TRANSACTION
END
问题是,无论日期是否重叠,都会发生错误。我做错了什么?
答案 0 :(得分:5)
本声明:
SELECT @startdate = inserted.startdate, @enddate = inserted.enddate,
@roomnumber = inserted.roomnumber
FROM inserted, bookinginfo
WHERE @roomnumber = bookinginfo.roomnumber AND
(@startdate BETWEEN bookinginfo.startdate AND bookinginfo.enddate) AND
(@enddate BETWEEN bookinginfo.startdate AND bookinginfo.enddate)
高度怀疑。您正在select
中分配变量,并在where
中使用相同的变量。将此表示为正常join
是否存在问题?
SELECT @startdate = i.startdate, @enddate = i.enddate, @roomnumber = i.roomnumber
FROM inserted i JOIN
bookinginfo bi
ON i.roomnumber = bi.roomnumber AND
(i.startdate BETWEEN bi.startdate AND bi.enddate) AND
(i.enddate BETWEEN bi.startdate AND bi.enddate) AND
i.BookinginfoID <> bi.BookinginfoID;
由于两个原因,这仍然无法满足您的需求。首先,这种逻辑是不正确的。第二,if
甚至没有使用它。我认为以下是你想要触发器的主体:
IF (EXISTS (SELECT 1
FROM inserted i JOIN
bookinginfo bi
ON i.roomnumber = bi.roomnumber AND
i.startdate <= bi.enddate AND
i.enddate >= bi.startdate AND
i.BookinginfoID <> bi.BookinginfoID;
)
BEGIN
RAISERROR ('Double bookings are not allowed',16,1)
. . .
END;
答案 1 :(得分:0)
这是因为最后IF EXISTS(SELECT * FROM inserted)
。 U不会在选定值之前检查,只检查每个未加特征的值。它会永远回报你。
看起来应该是这样的:
USE INL5
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_bookinginfo_doublebooking] ON [dbo].[bookinginfo]
FOR INSERT AS
DECLARE @startdate AS DATE
DECLARE @enddate AS DATE
DECLARE @roomnumber AS CHAR(3)
IF EXISTS(SELECT @startdate = inserted.startdate, @enddate = inserted.enddate, @roomnumber = inserted.roomnumber
FROM inserted, bookinginfo
WHERE @roomnumber = bookinginfo.roomnumber AND (@startdate BETWEEN bookinginfo.startdate AND bookinginfo.enddate) AND (@enddate BETWEEN bookinginfo.startdate AND bookinginfo.enddate) )
BEGIN RAISERROR ('Double bookings are not allowed',16,1)
ROLLBACK TRANSACTION
END