SQL - 将结果合并到while循环中

时间:2016-07-07 08:52:47

标签: sql loops while-loop

我想使用此代码获得单个结果

DECLARE
@count INT = 1,
@cutoff DATETIME = '12/31/2015'

while @count < 30
  begin
   select COUNT(Accnumber) from table
    where DATEDIFF(day,TranDateTime,@cutoff) <= 0
  SET @count = @count + 1
end
编辑:我已经修好了。我做的是我为表添加一个变量并插入其中并显示结果。抱歉英语不好:D

所以,

 DECLARE
    @count INT = 1,
    @cutoff DATETIME = '12/31/2015',
    *@tbl table(result int)*

    while @count < 30
      begin
     *insert into @tbl*
       select COUNT(Accnumber) from table
        where DATEDIFF(day,TranDateTime,@cutoff) <= 0
      SET @count = @count + 1
    end

*select * from @tbl*

我是新手,我不知道如何正确格式化答案。再次感谢您的回复。

0 个答案:

没有答案