SQL Server While-If循环中的代码错误

时间:2016-04-17 20:16:07

标签: sql sql-server if-statement while-loop

我知道还有其他帖子的代码可以解决我的问题,但我不想接受另一个代码,所以我试图自己做,而且我一直坚持不增加问题的月份,所以如果任何人都可以帮助我解决这个错误,这将是非常棒的。

问题是:

我必须填写从1990年到2016年的所有月份和日期的时间表,我已经实现了代码的工作,它正确地填充了年份和日期,但是月份增加到1月(1)然后是没有增加所以表格填满所有月份为1月(LOL)

这是我的代码:

create table Time
(
    Year int, 
    Month int,
    Day int
)

create procedure pTime
as
    declare @year int, @month int, @day int;
    set @year = 1990;
    set @month = 12;
    set @day = 10;

while(@year<=2016)
Begin

    If(@day = 29)
    Begin

        set @month = @month + 1;

        If(@month = 13)
        Begin

            set @month = 1;
            set @day = 1;
            set @year = @year + 1;

            insert into Time values (@year, @month, @day);
        End
    End 

    else
    Begin
        If(@day = 29)
        Begin

            set @month = @month + 1;
            set @day = 1;

            insert into Time values (@year, @month, @day);
        End

        Else    
        Begin

            insert into Time values (@year, @month, @day);

            set @day = @day + 1;
        End
    End
End

知道我的错误或建议在哪里?

3 个答案:

答案 0 :(得分:2)

我没有仔细查看您的错误,因为SQL Server有一些有用的日期算术功能。这是您的存储过程的简化版本:

create procedure pTime
as
  declare @theDate date = '12/10/1990', @days int = 0

while @theDate < '1/1/2016'
  begin
    insert into Time (Year, Month, Day) values (datepart(year, @theDate), datepart(month, @theDate), datepart(day, @theDate));
    set @theDate = dateadd(day, 1, @theDate)
  end

答案 1 :(得分:2)

另一种更快的方法是使用计数表。请注意以下代码:

WITH
E(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
         SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
         SELECT 1 UNION ALL SELECT 1),
iTally(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1))-1 FROM E a,E b,E c,E d,E e),
dates(dt) AS 
(
  SELECT TOP(datediff(DAY,'19900101','20160101')) DATEADD(day,N,'19900101') 
  FROM iTally
)
--INSERT [time] --uncomment for the insert, leave commented to see what will be inserted
SELECT YEAR(dt), MONTH(dt), DAY(dt)
FROM dates;

答案 2 :(得分:1)

为什么需要If(@year = 29)条件?在您的代码中,此块永远不会被执行。试试这个:

create procedure pTime
as
    declare @year int, @month int, @day int;
    set @year = 1990;
    set @month = 12;
    set @day = 10;

while(@year<=2016)
Begin
    If(@day = 29)
    Begin

        set @month = @month + 1;
        set @day = 1;

        If(@month = 13)
        Begin
            set @month = 1;            
            set @year = @year + 1;
            insert into Time values (@year, @month, @day);
        End
    End 

    else
    Begin
        If(@day = 29)
        Begin

            set @month = @month + 1;
            set @day = 1;

            insert into Time values (@year, @month, @day);
        End

        Else    
        Begin

            insert into Time values (@year, @month, @day);

            set @day = @day + 1;
        End
    End
End

我认为第一次作业set @day = 1;并不合适。增加@month值后,您还应将@day设置为1;