【问题标题】:Code error in a SQL Server While-If LoopSQL Server While-If 循环中的代码错误
【发布时间】:2016-08-09 11:11:54
【问题描述】:

我知道还有其他帖子的代码可以解决我的问题,但我不想使用其他人的代码,所以我尝试自己做,但我遇到了一个月没有增加问题的问题,所以如果任何人都可以帮助我解决这个错误,这将是很棒的。

问题是:

我必须用所有的月份和日期填充从 1990 年到 2016 年的表格时间,我已经实现了代码可以正常工作并且它正确填充了年份和日期,但月份增加到一月 (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

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

【问题讨论】:

  • “请调试我的代码”之类的问题在这里并不特别受欢迎。我认为您解决问题的方法不是很有效。我宁愿使用日期数据类型和 DateAdd 函数。 - 但是,当您想采用自己的方法时,请仔细研究一下 If(@year = 29) 条件。
  • 哦,我错过了。让我纠正一下。谢谢 :) 我不希望社区调试我的代码,我只是在寻找我所缺少的一些提示,仅此而已。

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


【解决方案1】:

我没有仔细检查您的错误,因为 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

【讨论】:

  • 你是他妈的上帝。这就是我所说的,不是调试我的代码,只是给我一些关于如何去做的提示。你真的很棒,伙计。我要研究这段代码。非常感谢,伙计:D
  • 感谢您的称赞,不客气。如果您有任何问题,请随时提问。如果对您有用,请接受答案。
【解决方案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;

【讨论】:

  • 这是与其他答案相比最快的方法
【解决方案3】:

为什么需要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;

【讨论】:

  • 这是我在这里写代码的错误,我已经编辑了帖子以更正它。
  • 哈!谢谢兄弟,我没有注意到这一点(我觉得有点愚蠢,哈哈)。你也很厉害:D
猜你喜欢
  • 1970-01-01
  • 2016-05-03
  • 2020-03-01
  • 2021-03-23
  • 1970-01-01
  • 2018-10-28
  • 2017-08-06
  • 2014-01-16
  • 1970-01-01
相关资源
最近更新 更多