以下是我的存储过程。它包含一个子查询
Select StartTime From DrTimings Where DrID = @DrID
如果此子查询返回多个值,则会出现错误。子查询返回多行。我想在光标中获取每个 @StartTime 和 @EndTime 。意味着我想“从 Doctor 获取下一个到 @StTime 和 @EndTime”
我可以在游标中使用两个参数吗?
ALTER PROCEDURE [dbo].SP_AFK_GetSlotsByDate
@DrID int,
@AppointmentDate Datetime
AS
BEGIN
Declare @StartTime Datetime
Declare @EndTime Datetime
BEGIN
SET @StartTime = (Select StartTime From DrTimings Where DrID = @DrID)
SET @EndTime = (Select EndTime From DrTimings Where DrID = @DrID)
END
DECLARE Doctor CURSOR FOR
Select StartTime from TimeList1 where StartTime>=@StartTime and StartTime<@EndTime
Declare @StTime datetime
Declare @SlotID int
Declare @AppointmentTime datetime
Declare @TempSlots Table (SlotID int , AppointmentTime datetime null)
Insert into
@TempSlots
(
SlotID ,
AppointmentTime
)
values(
0,
Getdate()
)
open Doctor
fetch next from Doctor into @StTime
while @@fetch_status = 0
Begin
Select @SlotID= T.SlotId from TimeList1 T
where T.StartTime>=@StartTime and T.StartTime<@EndTime and
T.SlotId not in
(Select A.SlotId from AppointmentSheet A where A.AppointmentDate=@AppointmentDate)
Select @AppointmentTime = Convert(varchar,right(T.StartTime,7),131)+' - '+ Convert(varchar,right(T.EndTime,7),131)
from TimeList1 T
where T.StartTime>=@StartTime and T.StartTime<@EndTime and
T.SlotId not in
(Select A.SlotId from AppointmentSheet A where A.AppointmentDate=@AppointmentDate)
Update @TempSlots
Set SlotID = @SlotID,
AppointmentTime=@AppointmentTime
fetch next from Doctor into @StTime
end
close Doctor
deallocate Doctor
Select * From @TempSlots
END