我有三张桌子。
表1.(预订)
CREATE TABLE [dbo].[Booking](
[Booking_Serno] [int] IDENTITY(1,1) NOT NULL,
[Dt] [datetime] NULL,
[start] [nvarchar](50) NULL,
[todate] [nvarchar](50) NULL,
[Service_Id] [int] NULL
) ON [PRIMARY]
INSERT [dbo].[Booking] ([Booking_Serno], [Dt], [start], [todate], [Service_Id]) VALUES (1, CAST(0x0000A6DA00000000 AS DateTime), N'9:30 AM', N'10:00 AM', 1)
GO
表2.(服务)
CREATE TABLE [dbo].[Service](
[Service_Serno] [int] IDENTITY(1,1) NOT NULL,
[Service_Duration] [int] NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Service] ON
INSERT [dbo].[Service] ([Service_Serno], [Service_Duration]) VALUES (1, 30)
表3(规则)
CREATE TABLE [dbo].[Rules](
[Rule_Serno] [int] IDENTITY(1,1) NOT NULL,
[Start_Dt] [varchar](50) NULL,
[End_Dt] [varchar](50) NULL,
[from_dt] [varchar](50) NULL,
[to_dt] [varchar](50) NULL,
[Service_Id] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Rules] ON
INSERT [dbo].[Rules] ([Rule_Serno], [Start_Dt], [End_Dt], [from_dt], [to_dt], [Service_Id]) VALUES (1, N'2016-07-02', N'2016-07-13', N'07:00', N'17:00', 1)
我正在运行存储过程。它得到了我想要的结果,但后来我试图通过改变间隔来预定时间,即使有预订的插槽,插槽显示为空。防爆。如果我设置一个60分钟的插槽并从7:00-8:00预订一个插槽它显示预订(xxx)但是当我将间隔更改为30时,7:00-8:00变为可用。它应该显示7:00-7:30和7:00-8:00不可用。 存储过程是
Dt:-12/12/2016; SERVICEID:-1
CREATE PROCEDURE [dbo].[RealGetFollowUp] @Dt varchar(50), @ServiceId int
AS
--declare @starttime datetime = '2015-10-28 12:00', @endtime datetime = '2015-10-28 14:00'
DECLARE @starttime varchar(50),
@endtime varchar(50),
@interval int
SELECT
@starttime = Rules.from_dt,
@endtime = Rules.to_dt,
@interval = Service.Service_Duration
FROM Service
INNER JOIN Rules
ON Service.Service_Serno = Rules.Service_Id
WHERE Service.Service_Serno = @ServiceId
--SELECT * INTO #tmp FROM d;
DECLARE @slots int
SELECT
@slots = DATEDIFF(MINUTE, @starttime, @endtime) / @interval
SELECT TOP (@slots)
N = IDENTITY(int, 1, 1) INTO #Numbers
FROM master.dbo.syscolumns a
CROSS JOIN master.dbo.syscolumns b;
SELECT
DATEADD(MINUTE, ((n - 1) * @interval), @starttime) AS start,
DATEADD(MINUTE, (n * @interval), @starttime) AS todate INTO #slots
FROM #numbers
SELECT
@Dt AS 'Date',
LEFT(CONVERT(varchar, s.start, 108), 10) AS Start,
LEFT(CONVERT(varchar, s.todate, 108), 10) AS 'End',
CASE
WHEN b.start IS NULL THEN '-'
ELSE 'xx'
END AS Status
FROM [#slots] AS s
LEFT JOIN Booking AS b
ON s.start = b.start
AND s.todate = b.todate
AND b.Dt = @Dt
DROP TABLE #numbers, #slots
GO
我需要检查预订表中是否预订了一个插槽,即使我更改了服务表中的间隔,预订表中预订的插槽也应显示为已预订。
答案 0 :(得分:1)
将sproc中的输出SELECT
更改为...
SELECT
@Dt AS 'Date',
LEFT(CONVERT(varchar, s.start, 108), 10) AS Start,
LEFT(CONVERT(varchar, s.todate, 108), 10) AS 'End',
CASE
WHEN b.start IS NULL THEN '-'
ELSE 'xx'
END AS Status
FROM [#slots] AS s
LEFT JOIN Booking AS b
ON (
--Range is bigger than the meeting
(s.start <= b.start
AND s.todate >= b.todate)
OR
--Range is smaller than the meeting
(s.start Between b.start and b.toDate
AND s.todate Between b.start and b.toDate)
)
AND b.Dt = @Dt