通过SQL Server中的触发器添加时隙

时间:2018-03-27 19:33:22

标签: sql-server time triggers schedule slot

每次医生在可用性表中插入他的可用性记录时,我都试图触发在时隙表中添加时隙。

我写了触发器,它保存了。但是当我将记录插入可用性时,触发器似乎没有被触发,但我没有任何错误消息。

有人能帮助我吗?非常需要。

以下是触发器的代码

ALTER TRIGGER [dbo].[trigger_addSlots] 
ON [dbo].[availability]
FOR INSERT AS
BEGIN
    DECLARE @AvailabilityId INT
    DECLARE @SlotStart DATETIME
    DECLARE @SlotEnd DATETIME
    DECLARE @NumberOfSlots INT
    DECLARE @Duration INT
    DECLARE @SlotDoctorId INT
    DECLARE @i INT

    SELECT @AvailabilityId = Id FROM inserted
    SELECT @SlotDoctorId = DoctorId FROM inserted
    SELECT @SlotStart = AvailableFrom FROM inserted
    SELECT @SlotEnd = AvailableTo FROM inserted 
    SELECT @Duration = AppointmentDuration FROM inserted

    SET @NumberOfSlots = CONVERT(INT, (@SlotEnd - @SlotStart)) / @Duration
    SET @i = 0;

    WHILE @i < @NumberOfSlots
    BEGIN
        INSERT INTO timeslots(AvailabilityId, SlotStart, SlotEnd, SlotDoctorId, IsAvailable) 
        VALUES (@AvailabilityId, @SlotStart, @SlotEnd, @SlotDoctorId, 1)

        SET @SlotStart = @SlotEnd
        SET @i = @i + 1
    END
END

0 个答案:

没有答案