0

我有一组脚本,我想循环多次,并将结果添加到临时表的 points_mth1、points_mth2 和 points_mth3 列中,但可能需要为尚未在表中的客户插入新行.

输入一个开始@EndDate,然后我想运行接下来的 2 个月,例如 2014-02-01、2014-03-01 和 2014-04-01

如何在下面添加一个循环以在接下来的 2 个月内运行?

SET NOCOUNT ON

DECLARE @EndDate DateTime

SET @EndDate = '2014-02-01' 

[set of scripts will be run]

然后将结果存储在一个临时表中,每次运行时,都会添加一个新列,例如.points_mth2,并将该数字存储在 Customerno 中,但客户不存在,然后插入 customerno:

IF object_id(N'tempdb..#temp', N'U') IS NOT NULL     DROP TABLE #temp
SELECT j1.customerno, SUM(points)[points_mth1]        ---> this will store the figures for the first date, then a new column [points_mth2] and [points_mth3]
into #temp
FROM customer j1
JOIN #customer_final j2 ON
j1.customerno= j2.customerno
GROUP BY j1.customerno
4

1 回答 1

0

不确定您到底想要什么,可能这是您的一个开始:

DECLARE @EndDate DATE = '2013-09-30 00:00:00:000';
DECLARE @tmpDate DATE = GETDATE();

WHILE @tmpDate <= @EndDate 
    BEGIN
        PRINT @tmpDate;

        -- do whatever you need here for specific date - @tmpDate

        SET @tmpDate = DATEADD(DAY, 1, @tmpDate);
    END
于 2013-08-09T04:54:29.030 回答