Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server While If循环中的代码错误_Sql_Sql Server_If Statement_While Loop - Fatal编程技术网

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

SQL Server While If循环中的代码错误,sql,sql-server,if-statement,while-loop,Sql,Sql Server,If Statement,While Loop,我知道还有其他帖子的代码可以解决我的问题,但我不想拿别人的代码,所以我试着自己去做,我被这个月没有增加的问题困扰着,所以如果有人能帮我解决这个错误,那就太棒了 问题是: 我必须用所有月份和天数填充从1990年到2016年的表时间,我已经实现了代码的工作,它正确填充了年份和天数,但月份增加到1月(1),然后不增加,因此表中填充了1月的所有月份(LOL) 这是我的密码: create table Time ( Year int, Month int, Day int )

我知道还有其他帖子的代码可以解决我的问题,但我不想拿别人的代码,所以我试着自己去做,我被这个月没有增加的问题困扰着,所以如果有人能帮我解决这个错误,那就太棒了

问题是:

我必须用所有月份和天数填充从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
创建表时间
(
整年,
整月,
日整数
)
创建过程时间
作为
声明@year int、@month int、@day int;
设置@year=1990;
设置@month=12;
设置@day=10;
而(@year为什么需要
如果(@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
创建过程pTime
作为
声明@year int、@month int、@day int;
设置@year=1990;
设置@month=12;
设置@day=10;
而(@year为什么需要
如果(@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
创建过程pTime
作为
声明@year int、@month int、@day int;
设置@year=1990;
设置@month=12;
设置@day=10;

而(@year我没有仔细查找您的错误,因为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
创建过程pTime
作为
声明@theDate日期='12/10/1990',@days int=0
而@theDate<'1/1/2016'
开始
插入时间(年、月、日)值(日期部分(年、日)、日期部分(月、日)、日期部分(日、日));
设置@theDate=dateadd(第1天@theDate)
结束

我没有仔细查找您的错误,因为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
创建过程pTime
作为
声明@theDate日期='12/10/1990',@days int=0
而@theDate<'1/1/2016'
开始
插入时间(年、月、日)值(日期部分(年、日)、日期部分(月、日)、日期部分(日、日));
设置@theDate=dateadd(第1天@theDate)
结束

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

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;

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

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;

“请调试我的代码”这类问题在这里并不特别受欢迎。我认为您解决问题的方法不是很有效。我更愿意使用日期数据类型和DateAdd函数。-但如果您想采用自己的方法,请仔细研究一下If(@year=29)条件。哦,我错过了。让我更正它。谢谢:)我不想让社区调试我的代码,我只是想寻找一些我缺少的提示,仅此而已。“请调试我的代码”这类问题在这里并不特别受欢迎。我认为您解决问题的方法不是很有效。我宁愿使用日期数据类型和DateAdd函数。-但由于您希望采用自己的方法,请仔细研究一下If(@year=29)条件。哦,我错过了。让我更正一下。谢谢:)我不想让社区调试我的代码,我只是想寻找一些我缺少的提示,仅此而已。这是我在这里编写代码的错误,我已经编辑了帖子来纠正它。哈!谢谢兄弟,我没有注意到这一点(我觉得有点傻,哈哈).你也真是太棒了:这是我在这里写代码的错误,我已经编辑了帖子来纠正它。哈!谢谢兄弟,我没有注意到这一点(我觉得有点傻,哈哈)1.你也真是太棒了f@ckin“天啊。这就是我说的,不是调试我的代码,只是给我一些提示。你真的很棒,哥们。我要学习这段代码。非常感谢,哥们:谢谢你的表扬,不客气。如果你有问题,尽管问。如果答案是正确的,请接受。”你能锻炼吗?你是一个f@ckin“天啊。这就是我说的,不是调试我的代码,只是给我一些如何做的提示。你真是太棒了,哥们。我要学习这段代码。非常感谢,哥们:谢谢你的表扬,不客气。如果你有问题,尽管问吧。如果它确实有效,请接受答案。”这是与其他答案相比最快的方法这是与其他答案相比最快的方法