---游标插入其他月空行有数据
---1.声明游标
Declare target_cursor Cursor Scroll
For
Select CustID
From #tTargetTmp
Group By CustID
Having Count(CustID) < 12;
--2.打开游标
Open target_cursor;
--3.声明游标提取数据所要存放的变量
Declare @CustId Int;
--4.定位游标到哪一行
Fetch First From target_cursor Into @CustId;
--into的变量数量必须与游标查询结果集的列数相同
While @@FETCH_STATUS = 0 --提取成功,进行下一条数据的提取操作
Begin
Insert Into #tMonthTarget
Select t.CustID, t.DATAMONTH, 0
From (Select CustID, DATAMONTH
From #tDateTmp a
Left Join #tMonthTarget b On 1 = 1
Where CustID = @CustId
Group By CustID, DATAMONTH) t
Left Join #tMonthTarget t1 On t.DATAMONTH = t1.BeginTime And t.CustID = t1.CustID
Where t1.TargetSum Is Null
Order By DATAMONTH;
Fetch Next From target_cursor Into @CustId; --移动游标
End;