SQL Server 2008:帮助优化使用游标的查询

| 我需要重写此内容,以免导致数据库溢出。我需要重写它,以便它不使用游标。 谢谢。     
已邀请:
更新这里有一些测试数据:
CREATE TABLE #TableB(SKU INT, BeginYear INT, EndYear INT, OptionA VARCHAR(10), OptionB VARCHAR(10))

INSERT INTO #TableB(SKU, BeginYear, EndYear, OptionA, OptionB)
VALUES  (1, 1920, 1950, \'option1\', \'option1\'),
        (1, 1980, 2001, \'option1\', \'option2\'),
        (1, 1940, 1952, \'option1\', \'option1\'), --overlapping years
        (2, 2001, 2005, \'option1\', \'option1\')

CREATE TABLE #TableA(SKU INT, OptionA VARCHAR(10), OptionB VARCHAR(10), Years INT)
然后,您可以尝试递归CTE:
;WITH CTE AS
(
    SELECT DISTINCT SKU, BeginYear, EndYear, OptionA, OptionB
    FROM #TableB
    UNION ALL
    SELECT SKU, BeginYear+1, EndYear, OptionA, OptionB
    FROM CTE 
    WHERE BeginYear < EndYear
)  
INSERT INTO #TableA(SKU, OptionA, OptionB, Years)
SELECT DISTINCT SKU, OptionA, OptionB, BeginYear
FROM CTE 
OPTION(MAXRECURSION 0)

SELECT *
FROM #TableA
ORDER BY SKU, OptionA, OptionB, Years
这里没有笛卡尔积。     
您可以轻松地将while循环替换为游标,如果您炸断了tempdb,请执行一些中间事务
declare @counter int

declare @temptable as tatble (RecID int Identity(1,1),
       SKU Varchar(25),
       OptionA Varchar(50),
       OptionB Varchar(50),
       CurrentYear Int,
       EndYear Int)

insert into @temptable
   Select SKU,OptionA, OptionB, BeginYear,EndYear
   From TableB

set @counter = (select max(recid) from @temptable)

begin transaction

while  @counter <> 0
Begin

    (***whatever sql logic***)

    if (@counter % 5000 = 0) and @@error = 0
        commit Transaction

    begin Transaction

    set @counter = @counter -1
End

commit transaction
    
可能会减少插入次数
Declare @temp table (
    SKU Varchar(25),
    OptionA Varchar(50),
    OptionB Varchar(50),
    [Year] Int
)

While @@FETCH_STATUS = 0
Begin
    While @CurrentYear <= @EndYear
    Begin
        Insert Into @temp(SKU, OptionA, OptionB, Years)
            Values(@SKU, @OptionA, @OptionB, @CurrentYear)                          

        Set @CurrentYear = @CurrentYear + 1
    End

    Insert Into TableA
        Select * From @temp

    Delete From @temp

    Fetch Next From @Row Into @SKU, @CurrentYear, @EndYear, @OptionA, @OptionB
End
    

要回复问题请先登录注册