光标运行一次并更新1值,应该运行多次。
sql server:值x具有一个id,我想将此ID用于值x与表中第二个值x之间的所有记录
示例
1 x 200 2年null 3年null 4 x 300 5年null 6年null
应该是 1 x 200 2年200 3年200 4 x 300 5年300 6年300
--truncate table #temp1
--select IDENTITY(int, 1, 1) AS RowID, nr *1 AS NR, '' updated
-- into #temp2
-- FROM [database].[schema].[table]
-- where seq_nr = 20
--select * from #temp2
DECLARE @FIRST NVARCHAR(MAX)
DECLARE @LAST NVARCHAR(MAX)
DECLARE @MAX_SEQ_GROEP NVARCHAR(MAX)
DECLARE outer_cursor CURSOR
FOR
SELECT [SEQ_NR_Group], FIRST_NUM, LAST_NUM
FROM (
(select 'A' T , Min(nr) FIRST_NUM from #temp2 where updated = '' )A FULL JOIN
(SELECT 'A' T ,NR LAST_NUM FROM #TEMP2
WHERE RowID IN (
select RowID +1 LAST_NUM
from #temp2
WHERE NR IN ( select Min(nr) FIRST_NUM
from #temp2
where updated = '')
AND updated = ''))B ON a.T=B.T
FULL JOIN (
SELECT 'A' T , MAX([SEQ_NR_Group])[SEQ_NR_Group] FROM (
SELECT DISTINCT CASE WHEN [SEQ_NR_Group] IS NULL THEN 1 ELSE MAX([SEQ_NR_Group])+1 END [SEQ_NR_Group]
FROM [database].[schema].[table]
GROUP BY [SEQ_NR_Group])a ) C ON C.T=A.T
)
SET @MAX_SEQ_GROEP = (SELECT MAX([SEQ_NR_Group]) FROM (
SELECT DISTINCT CASE WHEN [SEQ_NR_Group] IS NULL THEN 1 ELSE MAX([SEQ_NR_Group])+1 END [SEQ_NR_Group]
FROM [database].[schema].[table]
GROUP BY [SEQ_NR_Group])a)
OPEN outer_cursor
FETCH NEXT FROM outer_cursor INTO @MAX_SEQ_GROEP, @FIRST , @LAST
WHILE (@@FETCH_STATUS <> -1)
Begin ------------------------------------------> begin3
IF (@@FETCH_STATUS <> -2)
print @MAX_SEQ_GROEP
Print @FIRST
print @LAST
Begin ------------------------------------------> begin2
DECLARE INNER_CURSOR CURSOR LOCAL
FOR
SELECT * FROM (SELECT [SEQ_NR_Group], FIRST_NUM, LAST_NUM
FROM (
(select 'A' T , Min(nr) FIRST_NUM from #temp2 where updated = '' )A FULL
JOIN
(SELECT 'A' T ,NR LAST_NUM FROM #TEMP2 WHERE RowID IN (
select RowID +1 LAST_NUM
from #temp2
WHERE NR IN ( select Min(nr) FIRST_NUM
from #temp2
where updated = '')
AND updated = ''))B ON a.T=B.T
FULL JOIN (
SELECT 'A' T , MAX([SEQ_NR_Group])[SEQ_NR_Group] FROM (
SELECT DISTINCT CASE WHEN [SEQ_NR_Group] IS NULL THEN 1 ELSE MAX([SEQ_NR_Group])+1 END [SEQ_NR_Group]
FROM [database].[schema].[table]
GROUP BY [SEQ_NR_Group])a ) C ON C.T=A.T
) ) subquery
WHERE [SEQ_NR_Group] = @MAX_SEQ_GROEP
AND FIRST_NUM = @FIRST
ANd LAST_NUM = @LAST
FOR UPDATE
OPEN INNER_CURSOR
FETCH NEXT FROM INNER_CURSOR
print @FIRST print @last
WHILE (@@FETCH_STATUS <> -1)
Begin ------------------------------------------> begin1
IF (@@FETCH_STATUS <> -2)
begin
UPDATE [database].[schema].[table]
SET [SEQ_NR_Group] = @MAX_SEQ_GROEP
where 1=1
and NR BETWEEN @FIRST AND @LAST-1
UPDATE #temp2
SET updated=1
WHERE NR=@FIRST
end ------------------------------------------> End 1
FETCH NEXT FROM INNER_CURSOR
END ------------------------------------------> End 2
CLOSE INNER_CURSOR
DEALLOCATE INNER_CURSOR
Print 'done inner_cursor'
End ------------------------------------------> End 3
FETCH NEXT FROM outer_cursor INTO @MAX_SEQ_GROEP, @FIRST , @LAST
---here should run again and to update the next subset in the table.
End ------------------------------------------> End 4
CLOSE outer_cursor;
DEALLOCATE outer_cursor;
Print 'done outer_cursor'
查看代码