虽然循环在TSQL中达到条件后执行额外的操作

时间:2016-11-30 13:01:15

标签: sql-server tsql

下面的查询是对多个表的简单计算,并根据条件将数据插入另一个表

DECLARE @CCODE nvarchar(30);
DECLARE @OHQNTY INT;
DECLARE @Itemid nvarchar(30);
DECLARE @ITemlookupcode nvarchar(30);
DECLARE @ExtDescription nvarchar(200);
DECLARE @Department nvarchar(50);
DECLARE @QtyRCV INT;
DECLARE @DAYSCOUNT INT;
DECLARE @DateRecive nvarchar(50);
DECLARE @PoNumber nvarchar(50);



DECLARE CUR CURSOR FOR
SELECT [Code],[Onhand] FROM [dbo].[vwStockOnHand] WHERE code <300000;

OPEN CUR

FETCH NEXT FROM CUR INTO @CCODE,@OHQNTY
WHILE @@FETCH_STATUS = 0   
BEGIN 
    WHILE(@OHQNTY>0)
    BEGIN
        SELECT TOP(1) @Itemid=[Itemid],@PoNumber=[PoNumber],@ITemlookupcode=[ITemlookupcode],
                    @ExtDescription=[ExtDescription],
                    @Department=[Department],@QtyRCV=[QtyRCV],@DateRecive=[DateRecive]
                    FROM [dbo].[StockIn]
                    WHERE ITemlookupcode=@CCODE AND [QtyRCV]>0
                    ORDER BY DateRecive DESC, PoNumber ASC;

        SET @DAYSCOUNT= DATEDIFF(DAY,MAX(@DateRecive),GETDATE());

        IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0)
            BEGIN
                -- INSERTING DATA
                INSERT INTO [dbo].StockAging 
                (Itemid,ITemlookupcode,ExtDescription,Department,QtyRCV,DateRecive,DaysCount)
                VALUES(@Itemid,@ITemlookupcode,@ExtDescription,@Department,@OHQNTY,@DateRecive,@DAYSCOUNT)

                -- UPDATING  
                UPDATE  [dbo].[StockOnHand]
                SET [Onhand]=0 
                FROM [dbo].[StockOnHand]
                WHERE [Code]=@CCODE;

                UPDATE  [dbo].[StockIn]
                SET [QtyRCV]=0 
                FROM [dbo].[StockIn]
                WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

                SET @OHQNTY=0;
            END 
        ELSE IF (@OHQNTY > 0)
            BEGIN
                -- INSERTING DATA
                INSERT INTO [dbo].StockAging 
                (Itemid,ITemlookupcode,ExtDescription,Department,QtyRCV,DateRecive,DaysCount)
                VALUES(@Itemid,@ITemlookupcode,@ExtDescription,@Department,@QtyRCV,@DateRecive,@DAYSCOUNT) 

                -- UPDATING  
                UPDATE  [dbo].[StockOnHand]
                SET [Onhand]=[Onhand]-@QtyRCV FROM [dbo].[StockOnHand]
                WHERE [Code]=@CCODE;

                -- UPDATING  
                UPDATE  [dbo].[StockIn]
                SET [QtyRCV]=0 FROM [dbo].[StockIn]
                WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

                SET @OHQNTY=@OHQNTY-@QtyRCV;

            END
    END

    FETCH NEXT FROM CUR INTO @CCODE,@OHQNTY
END
    CLOSE CUR 
    DEALLOCATE CUR

我认为代码是自我解释的,

即使在达到条件后我面临的问题 @ OHQNTY&gt; 0 它再次通过循环。我知道循环有一些问题,但找不到我错过逻辑的地方。 这里Result是查询的结果,下面的RecvdQty = 9是附加的

2 个答案:

答案 0 :(得分:1)

IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0)

中的

你更新

UPDATE  [dbo].[StockIn]
SET [QtyRCV]=0 
FROM [dbo].[StockIn]
WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

但你已经&#34;使用&#34;只有@OHQNTY所以我认为你应该

UPDATE  [dbo].[StockIn]
SET [QtyRCV]=[QtyRCV]- @OHQNTY 
FROM [dbo].[StockIn]
WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

另外,你有这样的IF条件

IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0) BEGIN
...
END ELSE IF (@OHQNTY > 0) BEGIN
...
END

但是@OHQNTY > 0没用,应该是

IF (@QtyRCV >= @OHQNTY) BEGIN
...
END ELSE BEGIN
...
END

最后我会这样写它

IF (@QtyRCV >= @OHQNTY) BEGIN
  @QtyUsed = @OHQNTY
END ELSE BEGIN
  @QtyUsed = @QtyRCV
END

-- INSERTING DATA
INSERT INTO [dbo].StockAging 
(Itemid, ITemlookupcode, ExtDescription, Department, QtyRCV, DateRecive, DaysCount)
VALUES(@Itemid, @ITemlookupcode, @ExtDescription, @Department, @QtyUsed, @DateRecive, @DAYSCOUNT)

-- UPDATING  
UPDATE [dbo].[StockOnHand]
SET [Onhand] = [Onhand] - @QtyUsed
WHERE [Code]=@CCODE;

UPDATE [dbo].[StockIn]
SET [QtyRCV] = [QtyRCV] - @QtyUsed
WHERE ITemlookupcode=@CCODE AND PoNumber=@PoNumber;

SET @OHQNTY = @OHQNTY - @QtyUsed;

答案 1 :(得分:0)

我在If条款中看到了问题..

IF (@QtyRCV >= @OHQNTY AND @OHQNTY>0)
Begin
set @OHQNTY=0
End

If条款不满足时,你的while循环继续执行.. OHQNTY的移动设置到If Clause之外