如何解决如何在存储过程中的while循环之间进行比较?
如果要DATETIME
在一段时间内生成所有值,[@StartDate,
@EndDate]
则可以使用“统计”表:
SET NOCOUNT ON
GO
-- DROP TABLE dbo.Numbers
CREATE TABLE dbo.Numbers (
Num INT IDENTITY(0,1),
CONSTRAINT PK_Numbers_Num PRIMARY KEY (Num)
);
GO
-- It generates all values from 0 to 9999
INSERT dbo.Numbers DEFAULT VALUES
GO 10000 -- You can insert more numbers if diff. between those two date/time values is greather than 13 days (I used a frecv. of 2 minutes to computes this maximum limit)
然后是一个简单的SELECT
陈述
DECLARE @StartDate DATETIME2(0), @EndDate DATETIME2(0), @Frequency tinyint
SELECT @StartDate = '2015-04-17 11:00:00',
@EndDate = '2015-04-17 11:15:00',
@Frequency = 2; -- Minutes
SELECT n.Num,
DATEADD(MINUTE, n.Num*@Frequency, @StartDate) AS RangeStart
/*,
CASE
WHEN DATEADD(MINUTE, (n.Num + 1)*@Frequency, @StartDate) > @EndDate
THEN @EndDate
ELSE DATEADD(MINUTE, (n.Num + 1)*@Frequency, @StartDate)
END AS RangeEnd
*/
FROM dbo.Numbers n
WHERE n.Num <= DATEDIFF(MINUTE, @StartDate, @EndDate) / @Frequency
/*
Num RangeStart RangeEnd
----------- --------------------------- ---------------------------
0 2015-04-17 11:00:00 2015-04-17 11:02:00
1 2015-04-17 11:02:00 2015-04-17 11:04:00
2 2015-04-17 11:04:00 2015-04-17 11:06:00
3 2015-04-17 11:06:00 2015-04-17 11:08:00
4 2015-04-17 11:08:00 2015-04-17 11:10:00
5 2015-04-17 11:10:00 2015-04-17 11:12:00
6 2015-04-17 11:12:00 2015-04-17 11:14:00
7 2015-04-17 11:14:00 2015-04-17 11:15:00
*/
解决方法
请对此提供帮助,我一直在努力创造条件,让时间仅在24小时内。
所以我有这样的事情。谁能帮助我或告诉我条件是否正确。
我的 @DisplayStartTime = 11:00PM
基本上是我的 @nextStartTime = 11:00PM
OPEN cPhosLineTimeSlot
FETCH NEXT FROM cPhosLineTimeSlot
INTO @Parameter,@DisplayStartTime,@DisplayEndTime,@CodeEndTime,@Frequency,@Tolerance
WHILE @@FETCH_STATUS = 0
BEGIN
Declare @nextStartTime nvarchar(30)
set @nextStartTime = @DisplayStartTime
--insert into #ActualTimeSlot
--select @Parameter,@Tolerance
WHILE (convert(varchar,convert(time,@nextStartTime),100) < DATEADD(day,-1,GETDATE()))
BEGIN
SET @DisplayStartTime = @nextStartTime
SELECT @nextStartTime = ltrim(right(convert(nvarchar(100),DATEADD(minute,@nextStartTime)),8))
INSERT INTO #ActualTimeSlot
SELECT @Parameter,@nextStartTime,@Tolerance
SET @intFlag +=1
END
SET @intFlag = 1
FETCH NEXT FROM cPhosLineTimeSlot
INTO @Parameter,@Tolerance
END
CLOSE cPhosLineTimeSlot
DEALLOCATE cPhosLineTimeSlot
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。