0
您好I'have以下結果爲光標多次查詢插入使用光標
PONumber Code QuantityReceived
0200073 200757 4
0200073 201052 2
0200073 201435 2
0200073 200757 10
0200073 201056 1
0200073 202200 2
我使用的程序是
BEGIN
DECLARE @PN nvarchar(30);
DECLARE @TCODE nvarchar(50);
DECLARE @REQNTY INT;
DECLARE @LOTID nvarchar(50);
DECLARE @QUANTITY nvarchar(50);
DECLARE CUR CURSOR FOR
SELECT PONumber,Code,QuantityReceived
FROM Temp_stock2
--GROUP BY PONumber,Code,QuantityReceived
OPEN CUR
FETCH NEXT FROM CUR INTO @PN,@TCODE,@REQNTY
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN
WHILE(@REQNTY>0)
BEGIN
SELECT TOP(1)@LOTID=RR1038_LotEntry.ID,@QUANTITY=RR1038_LotEntry.Quantity
FROM [dbo].RR1038_LotEntry
INNER JOIN Temp_stock2 on Temp_stock2.LotID=RR1038_LotEntry.ID
WHERE RR1038_LotEntry.Quantity>0 AND [email protected]
ORDER BY RR1038_LotEntry.Quantity DESC;
IF (@QUANTITY >= @REQNTY)
BEGIN
-- UPDATING LOT ENTRY TABLE
UPDATE [dbo].RR1038_LotEntry
SET RR1038_LotEntry.Quantity = [email protected],LastUpdated=GETDATE()
FROM [dbo].RR1038_LotEntry
INNER JOIN Temp_stock2 on Temp_stock2.LotID=RR1038_LotEntry.ID
WHERE RR1038_LotEntry.ID [email protected] AND [email protected];
-- INSERTING THE SAME DATA TO LOT HISTORY TABLE
INSERT INTO [dbo].RR1038_LotHistory
(StoreID,LotEntryId,Quantity,ReferenceNumber,ReferenceType,LastUpdated)
SELECT
0 as StoreID ,Temp_stock2.LotID,
@REQNTY,
Temp_stock2.PurchaseOrderid as ReferenceNumber,
3 as ReferenceType,GETDATE() as LastUpdated
FROM [dbo].RR1038_LotEntry
INNER JOIN Temp_stock2 on Temp_stock2.LotID=RR1038_LotEntry.ID
WHERE RR1038_LotEntry.ID [email protected] AND [email protected];
SET @REQNTY=0;
END
ELSE
BEGIN
-- INSERTING THE SAME DATA TO LOT HISTORY TABLE
INSERT INTO [dbo].RR1038_LotHistory
(StoreID,LotEntryId,Quantity,ReferenceNumber,ReferenceType,LastUpdated)
SELECT
0 as StoreID ,Temp_stock2.LotID,
@REQNTY,
Temp_stock2.PurchaseOrderid as ReferenceNumber,
3 as ReferenceType,GETDATE() as LastUpdated
FROM [dbo].RR1038_LotEntry
INNER JOIN Temp_stock2 on Temp_stock2.LotID=RR1038_LotEntry.ID
WHERE RR1038_LotEntry.ID [email protected] AND [email protected];
SET @[email protected]@QUANTITY;
-- UPDATING LOT ENTRY TABLE
UPDATE [dbo].RR1038_LotEntry
SET RR1038_LotEntry.Quantity = 0,LastUpdated=GETDATE()
FROM [dbo].RR1038_LotEntry
INNER JOIN Temp_stock2 on Temp_stock2.LotID=RR1038_LotEntry.ID
WHERE RR1038_LotEntry.ID [email protected] AND [email protected];
END
END
END
FETCH NEXT FROM CUR INTO @PN,@TCODE,@REQNTY
END
CLOSE CUR
DEALLOCATE CUR
END
雖然我執行我得到的程序針對CODE的特定行的重複插入存在兩次。
當值不同(日期和中間的數值) – artm
你找到程序的任何問題,他們不重複? –
是的,你是對的,我一直在拍賣表中盲目搜索,沒有提供任何具體的輸入,改變了它。謝謝 –