我的数据库中有三个表:
Room (num PK, seats, reservation price) Reservation (num PK, client id FK Client(id), room number FK Room(num), reservation date) Client (id PK, name)
我想在预订之前,我们检查房间是否没有使用触发器。
这是我试过的脚本:
CREATE TRIGGER Verify_room_taken ON reservation
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS ( SELECT * FROM reservation
WHERE [room number] = (SELECT [room number] FROM INSERTED)
AND [reservation date] = (SELECT [reservation date] FROM INSERTED))
ROLLBACK TRAN
END
但是这个脚本不能正常工作,当我在不同的日期和不同的房间添加预订时,它不会因为光标而做出我所做的更改。
这是我第二次尝试的代码:
ALTER TRIGGER Verifier_chambre_prise ON reservation
INSTEAD OF INSERT
AS
BEGIN
IF NOT EXISTS ( SELECT * FROM reservation
WHERE [n° de chambre] = (SELECT [n° de chambre] FROM INSERTED)
AND [date de réservation] = (SELECT [date de réservation] FROM INSERTED))
INSERT INTO reservation SELECT * FROM INSERTED
END