0

我必须编写一个存储过程来查找给定的日期范围是否与表中的日期范围列表重叠。

前任)

数据库:SQL Server 2008 R2

SP Input: 2012-12-17 18:30:00.000       2012-12-19 18:29:59.000

SP Output(Out parameter): True/False

预订表样本数据:

FromDate                    ToDate

2012-12-11 18:30:00.000     2012-12-12 18:29:59.000
2012-12-12 18:30:00.000     2012-12-13 18:29:59.000
2012-12-10 18:30:00.000     2012-12-11 18:29:59.000
2012-12-18 18:30:00.000     2012-12-19 18:29:59.000
2012-12-23 18:30:00.000     2012-12-28 18:29:59.000
2012-12-17 18:30:00.000     2012-12-18 18:29:59.000
2012-12-19 18:30:00.000     2012-12-22 18:29:59.000
2012-12-16 18:30:00.000     2012-12-17 18:29:59.000
2012-12-13 18:30:00.000     2012-12-14 18:29:59.000
2012-12-11 23:00:00.000     2012-12-12 22:59:59.000

存储过程:

USE [BookingDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[FindConflict]    
    @FromDate datetime,
    @ToDate datetime,
    @IsConflict bit OUT
AS

BEGIN

?????????????????????(what to write here)

END

SQL:

USE [BookingDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BookingAdmin](
    [PK_BookingAdmin] [int] IDENTITY(1000,1) NOT NULL,
    [FromDate] [datetime] NOT NULL,
    [ToDate] [datetime] NOT NULL,
 CONSTRAINT [PK_BookingAdmin] PRIMARY KEY CLUSTERED 
(
    [PK_BookingAdmin] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[BookingAdmin] ON
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1048, CAST(0x0000A1240130DEE0 AS DateTime), CAST(0x0000A1250130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1049, CAST(0x0000A1250130DEE0 AS DateTime), CAST(0x0000A1260130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1050, CAST(0x0000A1230130DEE0 AS DateTime), CAST(0x0000A1240130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1051, CAST(0x0000A12B0130DEE0 AS DateTime), CAST(0x0000A12C0130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1052, CAST(0x0000A1300130DEE0 AS DateTime), CAST(0x0000A1350130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1053, CAST(0x0000A12A0130DEE0 AS DateTime), CAST(0x0000A12B0130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1055, CAST(0x0000A12C0130DEE0 AS DateTime), CAST(0x0000A12F0130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1056, CAST(0x0000A1290130DEE0 AS DateTime), CAST(0x0000A12A0130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1057, CAST(0x0000A1260130DEE0 AS DateTime), CAST(0x0000A1270130DDB4 AS DateTime))
INSERT [dbo].[BookingAdmin] ([PK_BookingAdmin], [FromDate], [ToDate]) VALUES (1058, CAST(0x0000A124017B0740 AS DateTime), CAST(0x0000A125017B0614 AS DateTime))
GO


CREATE PROCEDURE [dbo].[FindConflict]    
    @FromDate datetime,
    @ToDate datetime,
    @IsConflict bit OUT
AS
BEGIN
    if exists (select * from BookingAdmin where FromDate = @ToDate and ToDate = @FromDate)
        set @IsConflict = 1   
    else
        set @IsConflict = 0        
END
4

1 回答 1

1

如果我正确理解了您的需求,并且您只需要检查表中是否有完全相同的范围,您可以试试这个

USE [BookingDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[FindConflict]    
    @FromDate datetime,
    @ToDate datetime,
    @IsConflict bit OUT
AS
BEGIN
    if exists (select * from <your table> where FromDate = @FromDate and ToDate = @ToDate)
        set @IsConflict = 1   
    else
        set @IsConflict = 0
END

如果您想了解重叠范围,请更新,试试这个

 ...
    if exists (select * from <your table> where FromDate <= @ToDate and ToDate >= @FromDate)
        set @IsConflict = 1   
    else
        set @IsConflict = 0
 ...

SQL 提琴示例

于 2012-12-12T10:05:53.190 回答