Guest User

Untitled

a guest
Jun 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. Select StartTime From DrTimings Where DrID = @DrID
  2.  
  3. ALTER PROCEDURE [dbo].SP_AFK_GetSlotsByDate
  4. @DrID int,
  5. @AppointmentDate Datetime
  6. AS
  7. BEGIN
  8.  
  9. Declare @StartTime Datetime
  10. Declare @EndTime Datetime
  11. BEGIN
  12. SET @StartTime = (Select StartTime From DrTimings Where DrID = @DrID)
  13. SET @EndTime = (Select EndTime From DrTimings Where DrID = @DrID)
  14. END
  15.  
  16. DECLARE Doctor CURSOR FOR
  17.  
  18. Select StartTime from TimeList1 where StartTime>=@StartTime and StartTime<@EndTime
  19.  
  20. Declare @StTime datetime
  21. Declare @SlotID int
  22. Declare @AppointmentTime datetime
  23.  
  24. Declare @TempSlots Table (SlotID int , AppointmentTime datetime null)
  25.  
  26.  
  27. Insert into
  28. @TempSlots
  29. (
  30. SlotID ,
  31. AppointmentTime
  32. )
  33. values(
  34. 0,
  35. Getdate()
  36. )
  37.  
  38. open Doctor
  39. fetch next from Doctor into @StTime
  40. while @@fetch_status = 0
  41. Begin
  42.  
  43. Select @SlotID= T.SlotId from TimeList1 T
  44. where T.StartTime>=@StartTime and T.StartTime<@EndTime and
  45. T.SlotId not in
  46. (Select A.SlotId from AppointmentSheet A where A.AppointmentDate=@AppointmentDate)
  47.  
  48. Select @AppointmentTime = Convert(varchar,right(T.StartTime,7),131)+' - '+ Convert(varchar,right(T.EndTime,7),131)
  49. from TimeList1 T
  50. where T.StartTime>=@StartTime and T.StartTime<@EndTime and
  51. T.SlotId not in
  52. (Select A.SlotId from AppointmentSheet A where A.AppointmentDate=@AppointmentDate)
  53.  
  54. Update @TempSlots
  55. Set SlotID = @SlotID,
  56. AppointmentTime=@AppointmentTime
  57.  
  58. fetch next from Doctor into @StTime
  59. end
  60. close Doctor
  61. deallocate Doctor
  62.  
  63. Select * From @TempSlots
  64. END
  65.  
  66. fetch next from Doctor into @StTime, @EndTime
  67.  
  68. select StartTime, EndTime
  69. from TimeList1
  70. where StartTime>=@StartTime and StartTime<@EndTime
  71.  
  72. ALTER PROCEDURE [dbo].SP_AFK_GetSlotsByDate
  73. (
  74. @DrID int,
  75. @AppointmentDate DateTime
  76. )
  77. AS
  78.  
  79. DECLARE
  80. @StartTime DateTime,
  81. @EndTime DateTime,
  82. @SlotID int,
  83. @AppointmentTime DateTime;
  84.  
  85. -- Retrieve the initial values for StartTime and EndTime
  86. -- These values get overwritten by the cursor (?)
  87. SELECT
  88. @StartTime = StartTime,
  89. @EndTime = EndTime
  90. FROM
  91. DrTimings
  92. WHERE
  93. DrID = @DrID;
  94.  
  95.  
  96. DECLARE @TempSlots TABLE
  97. (
  98. SlotID int,
  99. AppointmentTime datetime NULL
  100. );
  101.  
  102. -- Set default values
  103. INSERT @TempSlots (SlotID,AppointmentTime)
  104. VALUES (0, GETDATE());
  105.  
  106.  
  107. DECLARE Doctor CURSOR FOR
  108. SELECT
  109. StartTime,
  110. EndTime
  111. FROM
  112. TimeList1
  113. where
  114. StartTime >= @StartTime AND
  115. StartTime < @EndTime;
  116.  
  117. OPEN Doctor
  118. FETCH NEXT FROM Doctor INTO @StartTime,@EndTime
  119. WHILE @@FETCH_STATUS = 0
  120. BEGIN
  121. SELECT
  122. @SlotID = T.SlotId,
  123. @AppointmentTime = CONVERT(varchar,RIGHT(T.StartTime,7),131)
  124. + ' - ' + CONVERT(varchar,RIGHT(T.EndTime,7),131)
  125. FROM
  126. TimeList1 T
  127. LEFT JOIN AppointmentSheet A ON T.SlotId = A.SlotId
  128. WHERE
  129. T.StartTime >= @StartTime AND
  130. T.StartTime < @EndTime AND
  131. A.AppointmentDate = @AppointmentDate AND
  132. A.SlotId IS NULL;
  133.  
  134. -- This table will always be updated to contain the latest values
  135. -- it will contain only one row
  136. UPDATE
  137. @TempSlots
  138. SET
  139. SlotID = @SlotID,
  140. AppointmentTime = @AppointmentTime;
  141.  
  142. FETCH NEXT FROM Doctor INTO @StartTime,@EndTime
  143. END
  144.  
  145. CLOSE Doctor
  146. DEALLOCATE Doctor
  147.  
  148. -- Return results
  149. SELECT
  150. SlotId,
  151. AppointmentTime
  152. FROM
  153. @TempSlots;
Add Comment
Please, Sign In to add comment