📜  sql 游标示例多列 - SQL 代码示例

📅  最后修改于: 2022-03-11 15:05:28.354000             🧑  作者: Mango

代码示例1
DECLARE @OrderNo NVARCHAR(50)
       DECLARE @PoNo NVARCHAR(50)
       DECLARE @ItemNo NVARCHAR(50)
       DECLARE @BarcodeNo NVARCHAR(250)
       DECLARE @CartonNo NVARCHAR(250)
       DECLARE @Qty INT
       DECLARE @CNT INT
       DECLARE ADIDAS_CURSOR CURSOR FOR

       SELECT ORDERNUMBER,PONUMBER,ITEMLINENUMBER, Quantity, UCCNumber, CartonNumber
       FROM [MASTER] WITH (NOLOCK)
       WHERE HasPackingInfo = 0

       OPEN ADIDAS_CURSOR
       FETCH NEXT FROM ADIDAS_CURSOR INTO @OrderNo, @PoNo, @ItemNo, @Qty, @BarcodeNo, @CartonNo
       WHILE @@FETCH_STATUS =0
       BEGIN
             SELECT @Qty= Quantity, @BarcodeNo = BarcodeNumber, @CartonNo = CartonNumber
             FROM Shipment_856N  WITH (NOLOCK)
             WHERE SONumber= @OrderNo AND PONumber=@PoNo AND ItemLineNumber=@ItemNo
             SET @CNT = (SELECT COUNT(*) FROM Shipment_856N  WITH (NOLOCK)
             WHERE SONumber= @OrderNo AND PONumber=@PoNo AND ItemLineNumber=@ItemNo)

             IF @CNT > 0 

             BEGIN
                 UPDATE [MASTER] SET HasPackingInfo=1, Quantity=@Qty, UCCNumber=@BarcodeNo, CartonNumber=@CartonNo
                 WHERE OrderNumber = @OrderNo AND PONumber=@PoNo AND ItemLineNumber=@ItemNo
             END

             FETCH NEXT FROM ADIDAS_CURSOR INTO @OrderNo, @PoNo, @ItemNo,  @Qty, @BarcodeNo, @CartonNo
       END

       CLOSE ADIDAS_CURSOR
       DEALLOCATE ADIDAS_CURSOR