我有在 table 中插入行的查询,这个 table 有添加触发器,当我 运行 这个查询这个触发器引发错误如下:

I have query which insert rows in table, this table have add trigger, when I run this query this trigger raise error as follows:

错误

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Error Number: 512
Error Severity: 16
Error State: 1

Error Procedure: AddBookingSlots

代码:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[AddDoctorBookingSlots]
ON [dbo].[BookingDay]
AFTER INSERT AS
BEGIN
    DECLARE @starttime AS DATETIME = (SELECT MyDateTime1 = CAST([Day] AS DATETIME) + CAST(DayFrom AS DATETIME)  
                                      FROM INSERTED)
    DECLARE @endtime AS DATETIME = (SELECT MyDateTime1 = CAST([Day] AS DATETIME) + CAST(DayTo AS DATETIME)  
                                      FROM INSERTED)
    DECLARE @waitingtime AS INT = (SELECT WaitingTime FROM INSERTED)

    WHILE (@starttime <= @endtime)
    BEGIN
        INSERT INTO [dbo].[Booking] ([DoctorID], [Day], [Time], [Fees], [Valid],
                                     [PatientPhone], [BookingDayID],[PatientName], [PatientEmail],
                                     [NoShow], [PackageID], [IsFree],[IsEditable],
                                     [Shift], [CancelledByDoctor],[CancelledByUser],
                                     [UserId], [UpdatedBy])
        VALUES ((SELECT DoctorID FROM INSERTED),
                (SELECT [Day] FROM INSERTED),
                (SELECT CONVERT(CHAR(5), @starttime, 108)),
                (SELECT ExaminationFees FROM Doctor 
                 WHERE DoctorID = (SELECT DoctorID FROM INSERTED)), 1, NULL,
                (SELECT BookingDayID FROM INSERTED), NULL, NULL, 0,
                (SELECT [SubscribtionPackage] FROM Doctor 
                 WHERE DoctorID = (SELECT DoctorID FROM INSERTED)), 0, 0,
                (SELECT [Shift] FROM INSERTED), 0, 0,
                (SELECT UserId FROM INSERTED),
                (SELECT UpdatedBy FROM INSERTED))

        SET @starttime = DATEADD(minute, @waitingtime, @starttime)
    END
END

Inserted 可能包含许多行 - 这是错误的原因。您可能应该放弃值插入方法,并可能像这样

替换为 select 插入方法
SELECT I.DoctorID 
           ,i.[Day] 
            , convert(char(5), @starttime, 108)
           ,d.ExaminationFees
           ,1
           ,null
           ,i.BookingDayID 
           ,null
           ,null
           ,0
           ,d.[SubscribtionPackage]
           ,0
           ,0
           ,i.[Shift]
           ,0,0,
           i.UserId,
           i.UpdatedBy
From INSERTED i
join doctor d on d.DoctorID=i.doctorid

我不知道 doctor 与 insert 有何关系,因此您可能需要根据自己的需要进行调整。