我有3张桌子预订,票务和座位。它们的连接方式类似于Reservation.code_of_ticket = Ticket.code和Ticket.number_of_seat = Seat.number。在表Ticket是属性职业中,当我向表Reservation中插入数据时需要更改它。这可能吗?
CREATE TRIGGER occupancy
ON Reservation
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE Seat
INNER JOIN Seat ON Seat.number = Ticket.number_of_seat
INNER JOIN Ticket ON Ticket.code = Reservation.code_of_ticket
SET occupancy = 'reserved'
WHERE occupancy = 'free';
END
答案 0 :(得分:3)
您的基本逻辑是正确的,语法非常错误。在处理触发器时,我们使用特殊表inserted
和deleted
,它们具有数据更改的信息,并具有与基本表相同的结构。
触发器应如下所示:
CREATE TRIGGER occupancy
ON Reservation
AFTER INSERT
AS
SET NOCOUNT ON;
UPDATE s --This is an alias for the table Seat
SET occypancy = 'reserved'
FROM Seat s --We can use FROM clauses like a normal SELECT
INNER JOIN Ticket t on s.number = t.number_of_seat --Is the seat number global or should you add a theater/auditorium number in your join logic?
INNER JOIN inserted i on t.code = i.code_of_ticket
WHERE s.occypancy = 'free';