SQL Cursor中的代码只会执行第一个FETCH

时间:2013-08-30 14:56:03

标签: sql sql-server tsql

我有一个SQL游标,我遇到了问题。当我从游标内部删除IF @debug = 1语句时,只会更新FETCH中的第一条记录,但如果我离开IF @debug = 1,则会更新所有必需的记录。任何关于为什么会发生这种情况的想法,我知道我的Cursor最有可能出现问题?代码如下:

DECLARE Verify_Shipment_Cur CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
    SELECT DISTINCT lpd_shipment_id, lpd_po_number, lpd_customer_id, lpd_sku, lpd_lottable01, lpd_lottable02, lpd_lottable03, lpd_putaway_zone, lpd_pdt
        FROM PO_DETAIL01(NOLOCK) 
    WHERE lpd_shipment_id = @i_SHIPMENT_ID 
            AND lpd_po_number = @i_POKEY 
            AND lpd_customer_id = @i_CUSTOMER_ID
            AND lpd_status = @AvailableStatus

OPEN Verify_Shipment_Cur
    WHILE @ShipmentSKUCount >= @ShipmentSKUCountCur
        BEGIN
            FETCH NEXT FROM Verify_Shipment_Cur INTO @ShipmentID, @POKey, @CustomerID, @SKU, @Lottable01, @Lottable02, @Lottable03, @PutawayZone, @PDT

            IF EXISTS(SELECT 1 FROM PO_DETAIL(NOLOCK) WHERE pd_asn_number = @i_SHIPMENT_ID AND pd_po_number = @i_POKEY 
                        AND pd_sku = @SKU AND pd_type = @ShmtType AND pd_ordered_qty <> pd_received_qty)
                BEGIN
                    UPDATE PO_DETAIL
                        SET pd_adjusted_qty = pd_ordered_qty - pd_received_qty
                    WHERE pd_asn_number = @i_SHIPMENT_ID 
                        AND pd_po_number = @i_POKEY 
                        AND pd_sku = @SKU 
                        AND pd_type = @ShmtType
                END

            UPDATE PO_DETAIL
                SET pd_lottable01 = @Lottable01
                    , pd_lottable02 = @Lottable02
                    , pd_lottable03 = @Lottable03
                    , pd_lottable04 = ''
                    , pd_lottable05 = @Date
                    , pd_putaway_zone = @PutawayZone
                    , pd_pdt = @PDT
                    , pd_status = @VerifiedStatus
            WHERE pd_asn_number = @i_SHIPMENT_ID 
                        AND pd_po_number = @i_POKEY 
                        AND pd_sku = @SKU 
                        AND pd_type = @ShmtType


            UPDATE PO_DETAIL01
                SET lpd_status = @VerifiedStatus
            WHERE lpd_shipment_id = @i_SHIPMENT_ID 
                    AND lpd_po_number = @i_POKEY 
                    AND lpd_customer_id = @i_CUSTOMER_ID
                    AND lpd_status = @AvailableStatus

        IF @debug = 1
            BEGIN
                SELECT @ShipmentSKUCount AS SKUCOUNT
                , @ShipmentSKUCountCur AS SKUCOUNTCUR
                , @SKU AS SKU
                , @ShipmentID AS SHIPMENT
                , @POKey AS POKEY
            END

            SET @ShipmentSKUCountCur = @ShipmentSKUCountCur + 1

        END
CLOSE Verify_Shipment_Cur
DEALLOCATE Verify_Shipment_Cur

2 个答案:

答案 0 :(得分:1)

我看起来不错,但我显然没有你的数据来帮助。我是否可以建议在光标的各个部分放置一些打印语句。这样你就可以看到代码实际上是如何流动的。它没有帮助,但这就是我会做的。

答案 1 :(得分:0)

请问,您是否可以尝试定义两个变量:set @ShipmentSKUCount=[initial value]set @ShipmentSKUCountCur=[initial or constant value],并看看会发生什么?
另外,我发现没有检查@@FETCH_STATUS。它也可能导致两次或更多次读取同一行 请提供反馈。