如何将光标写入当前日期连续15天添加一天 [英] How to write cursor to add one day in current date continuous 15 times
本文介绍了如何将光标写入当前日期连续15天添加一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写光标,连续15天在当前日期添加一天。
今天是2014年7月5日然后下一次
8/5 / 2014
9/5/2014等等..
I want to write cursor for adding one day in current date 15 times continuously.
Like today is 7/5/2014 then next time
8/5/2014
9/5/2014 and so on..
推荐答案
游标用于逐步查看返回的查询结果多个结果,例如表格中的行。
要做你所描述的,你最好使用WHILE循环...这样的事情(警告,此代码未经测试)
A cursor is used to step through the results of a query that returns multiple results e.g. rows from a table.
To do what you are describing you would be better off with a WHILE loop ... something like this (warning, this code is untested)
DECLARE @LoopCount INT
SET @LoopCount = 0
DECLARE @calcDate DateTime
SET @calcDate = GETDATE() -- start the dates output with today
DECLARE @outDate varchar(30) -- will be used for output only
WHILE @LoopCount < 15
BEGIN
SET @outDate = CAST(calcDate AS varchar(30))
RAISERROR (@outDate, 0, 1) WITH NOWAIT -- same as PRINT but immediately output
SET @calcDate = DATEADD(dd, 1, @calcDate) -- Add one day to the current date
END
注意W HILE不具备性能,但这里只有15次迭代。
Note that WHILE is not performant, but there are only 15 iterations here.
这样的东西:
Something like this:
WITH q AS
(
SELECT GETDATE() AS datum
UNION ALL
SELECT datum + 1
FROM q
WHERE datum +1 < GETDATE() + 15
)
SELECT DATEADD(dd, DATEDIFF(dd, 0, datum), 0) AS MyDate
FROM q
这篇关于如何将光标写入当前日期连续15天添加一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文