只是想發佈我的查詢在這裏 - 可能有助於某人。奇蹟般有效。謝謝您的幫助!
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PS_Event_Insert]
(
@UserID int,
@AmenityID int,
@DateFrom datetime,
@DateTo datetime,
@TimeFrom nvarchar(50),
@TimeTo nvarchar(50),
@Description nvarchar(max),
@IsPrivate bit,
@NumberOfPeople nvarchar(100),
@Food bit,
@StatusID int,
@Notes nvarchar(500) = NULL,
@EventID int OUTPUT
)
AS
declare @newdtfrom datetime = cast(@DateFrom + ' ' + @TimeFrom as datetime)
declare @newdtto datetime = cast(@DateTo + ' ' + @TimeTo as datetime)
DECLARE @RecCount int
BEGIN
SET @RecCount = (
select count(*) from [events]
where (@newdtfrom >= cast([datefrom] + ' ' + [timefrom] as datetime) and @newdtfrom <= cast([dateto] + ' ' + [timeto] as datetime))
or (@newdtto > cast([datefrom] + ' ' + [timefrom] as datetime) and @newdtto <= cast([dateto] + ' ' + [timeto] as datetime))
or (@newdtfrom <= cast([datefrom] + ' ' + [timefrom] as datetime) and @newdtto >= cast([dateto] + ' ' + [timeto] as datetime))
AND
[AmenityID] = @AmenityID
AND
[StatusID] = 5
)
IF (@RecCount = 0)
BEGIN
INSERT INTO [Events]
(
[UserID],
[AmenityID],
[DateFrom],
[DateTo],
[TimeFrom],
[TimeTo],
[Description],
[IsPrivate],
[NumberOfPeople],
[Food],
[StatusID],
[Notes]
)
VALUES
(
@UserID,
@AmenityID,
@DateFrom,
@DateTo,
@TimeFrom,
@TimeTo,
@Description,
@IsPrivate,
@NumberOfPeople,
@Food,
@StatusID,
@Notes
)
SET @EventID = SCOPE_IDENTITY()
END
ELSE
BEGIN
SET @EventID = -999
END
END
您還需要測試完整覆蓋範圍 – 2011-12-26 19:50:20
謝謝各位!非常感激。 :) – user1100221 2011-12-26 22:21:06