我需要帮助优化这个存储过程。如果可能没有光标

时间:2013-10-20 14:06:55

标签: tsql stored-procedures group-by cursor

Declare @CurrentDate   varchar(25)
Declare @PreviousDate  varchar(25)
Declare @MaxDate       varchar(25)

Declare Cursor1 Cursor  For
Select Distinct ProductID
    From   Products
    Where  ProductId Is Not Null
    Order By ProductId

Declare @ProductID varchar(14)

Open Cursor1
Fetch Next From Cursor1 Into @ProductId
While @@Fetch_Status = 0
Begin
    Select @MaxDate = Max(ProductDate)
    From   Products
    Where  ProductId = @ProductID

    Declare Cursor2 Cursor For 
    Select ProductDate 
    From   Products
    Where  ProductId = @ProductID    
    Order  By ProductDate

    Open Cursor2
    Fetch Next From Cursor2 Into @CurrentDate
    Begin Try
        While (@@Fetch_Status = 0) 
        Begin
            If @PreviousDate Is Not Null And 
               @CurrentDate Is Not Null And 
               @PreviousDate != @MaxDate
            Begin
                Update Products 
                Set ProductEndDate = @CurrentDate 
                Where ProductId = @ProductId 
                And ProductDate = @PreviousDate  
            End                 
            Set @PreviousDate  = @CurrentDate

            Fetch Next From Cursor2 Into @CurrentDate   
        End
        Close Cursor2
        Deallocate Cursor2
    End Try
    Begin Catch
        Declare @error INT,@message varchar(4000);
        Select 
            @error = error_number(), 
            @message = error_message();
        Close Cursor2
        Deallocate Cursor2
    End Catch
End 
Close Cursor1
Deallocate Cursor1

1 个答案:

答案 0 :(得分:1)

如果您有重复的ProductID, ProductDate组合,这可能会有所不同,但它可能会让您走上正轨:

Martin Smith简化

;With cte as (
    Select
        ProductID,
        ProductEndDate,
        ProductDate,
        row_number() over (partition by ProductID order By ProductDate) rn
    From
        Products
)
Update
    x
Set
    x.ProductEndDate = y.ProductDate
From
    cte x
        inner join
    cte y
        on x.ProductID = y.ProductID and
           x.rn = y.rn - 1;

<强> Example SQLFiddle

ProductId, ProductDate上的索引可以加快速度。

相关问题