我试图将@date中的结果插入临时表,这是我的合作
DECLARE @MinDate DATE = CONVERT(VARCHAR(15), DATEADD(month, DATEDIFF(MONTH, 0, GETDATE()), 0), 101),
@MaxDate DATE = CONVERT(VARCHAR(15), EOMONTH(GETDATE()), 101),
@dayInMonth VARCHAR(15);
DECLARE @date DATE;
DECLARE @counter INT = 0;
DECLARE my_cursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR
SELECT TOP
(Datediff(day, @MinDate, @MaxDate) + 1) Date = Dateadd(day, Row_number() OVER(ORDER BY a.object_id) - 1, @MinDate)
FROM
sys.all_objects a
CROSS JOIN
sys.all_objects b;
OPEN my_cursor
FETCH next FROM my_cursor INTO @date
WHILE @@FETCH_STATUS = 0
BEGIN
IF( @counter = 15 )
BEGIN
--PRINT @date
--PRINT @date -- here is where you get the 15th date
IF DAY(@date) BETWEEN 10 AND 20
BEGIN
SET @date = DATEADD(DAY,(15-DATEPART(DAY,@date)),@date)
PRINT CONVERT(VARCHAR(15), @date, 101)
SET @counter = 0
END
IF @counter = 0
BEGIN
SET @date = DATEADD(DAY,(30-DATEPART(DAY,@date)),@date)
PRINT CONVERT(VARCHAR(15), @date, 101)
END
END
SET @counter = @counter + 1
FETCH next FROM my_cursor INTO @date
END
CLOSE my_cursor
DEALLOCATE my_cursor
无论如何,我可以将结果插入临时表吗?如果是这样......怎么样?
任何帮助都可以。谢谢!
答案 0 :(得分:2)
如果我理解你的问题,这应该有用。
DECLARE @MinDate DATE = CONVERT(VARCHAR(15),DATEADD(month, DATEDIFF(MONTH, 0, GETDATE()), 0), 101),
@MaxDate DATE = CONVERT(VARCHAR(15),EOMONTH(GETDATE()),101),
--DECLARE @MinDate DATE = '20170301',
-- @MaxDate DATE = '20170331',
@dayInMonth VARCHAR(15);
DECLARE @date DATE;
DECLARE @counter INT = 0;
DECLARE my_cursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR
SELECT TOP (Datediff(day, @MinDate, @MaxDate) + 1) Date =
Dateadd(day, Row_number()
OVER(
ORDER
BY a.object_id) - 1, @MinDate)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b;
Create table #temp (datevar varchar(15))
OPEN my_cursor
FETCH next FROM my_cursor INTO @date
WHILE @@FETCH_STATUS = 0
BEGIN
IF( @counter = 15 )
BEGIN
--PRINT @date
--PRINT @date -- here is where you get the 15th date
IF DAY(@date) BETWEEN 10 AND 20
BEGIN
SET @date = DATEADD(DAY,(15-DATEPART(DAY,@date)),@date)
insert into #temp values (CONVERT(VARCHAR(15), @date, 101))
PRINT CONVERT(VARCHAR(15), @date, 101)
SET @counter = 0
END
IF @counter = 0
BEGIN
SET @date = DATEADD(DAY,(30-DATEPART(DAY,@date)),@date)
insert into #temp values (CONVERT(VARCHAR(15), @date, 101))
PRINT CONVERT(VARCHAR(15), @date, 101)
END
END
SET @counter = @counter + 1
FETCH next FROM my_cursor INTO @date
END
CLOSE my_cursor
DEALLOCATE my_cursor
select * from #temp