我需要创建触发器,该触发器将检查表中是否存在具有相同时间和相同剧院ID的值
CREATE TRIGGER trCheckSameTime
ON dbo.Poster
FOR INSERT,UPDATE
AS
IF EXISTS(SELECT P.Id_Poster
FROM Poster P, inserted I
WHERE P.Date = I.Date AND P.Id_Theater = I.Id_Theater)
BEGIN
ROLLBACK TRANSACTION
RAISERROR('There can''t be two spectacles at the same time in the same theater', 16, 10)
END
我正在尝试使用它,所以当我输入
时就想要它INSERT INTO Poster
VALUES (1,4,1,'20190420 16:00:00')
INSERT INTO Poster
VALUES (1,4,1,'20190420 16:00:00')
要触发它禁止这样做,但是此触发器禁止在表中输入任何数据。
表中的第三个值是Theater_id,第四个值是日期,
答案 0 :(得分:3)
您需要先检查数据插入为
*s2 == 0
您还可以创建CREATE TRIGGER trCheckSameTime
ON dbo.Poster
INSTEAD OF INSERT, UPDATE
AS
IF NOT EXISTS(
SELECT 1
FROM dbo.Poster P INNER JOIN INSERTED I
ON P.Id_Theater = I.Id_Theater
AND
P.[Date] = I.[Date]
)
BEGIN
INSERT INTO dbo.Poster(<Columns Here>)
SELECT <Columns Here> FROM INSERTED;
END
ELSE
BEGIN
RAISERROR('There can''t be two spectacles at the same time in the same theater', 16, 10)
END
,这是解决问题的更好方法。
constraint