项目临时表#TMPxmdt 存有ID,起始年度,完成年度(int型)两个字段;
实现功能:将#TMPxmdt表中每个ID对应的起始年度至完成年度中所有年度以(ID, ND)的形式插入另一个临时表#TMPdt
DECLARE @QSND INT ,@WCND INT DECLARE @ID VARCHAR(36) DECLARE cur CURSOR FOR SELECT Convert(VARCHAR(36),Project_Id) AS ID,Convert(INT ,substring(qdsj, 1, 4)) AS qsnd, Convert(INT ,substring(wcsj, 1, 4)) AS wcsj FROM #TMPxmdt OPEN cur FETCH NEXT FROM cur INTO @ID, @QSND ,@WCND WHILE ( @@fetch_status = 0 ) BEGIN DECLARE @nd INT set @nd=@QSND WHILE(@nd<=@WCND) BEGIN INSERT INTO #TMPdt VALUES(@ID, Convert(VARCHAR(4), @nd)) set @nd=@nd+1 END FETCH NEXT FROM cur INTO @ID, @QSND ,@WCND END CLOSE cur DEALLOCATE cur