0
我有一個存儲過程,它從用戶獲取兩個參數,即referenceNumber
和destinationLocation
,其中referenceNumber
是該行的唯一標識符,而destinationLocation
是請求的物品將被傳送到的位置。請參閱下表tblStockMove
:每行返回執行存儲過程?
deliveryID | ProductID | SourceLocation | DestinationLocation | Quantity | deliveryStart | deliveryEnd | ReferenceNumber | Status | Requestor | Receiver | ControlNumber | MoveType | ProductCode
13 | 1 | WAREHOUSE | Burger Queen | 5 | 2016-11-14 | NULL | CTRL_MULTI01 | PENDING| john.doe | NULL | CTRL_MULTI01 | MULTIPLE | 19000207
14 | 1 | WAREHOUSE | DcMo | 4 | 2016-11-14 | NULL | CTRL_MULTI01 | PENDING| john.doe | NULL | CTRL_MULTI01 | MULTIPLE | 19000207
15 | 1 | WAREHOUSE | Strapbucks | 10 | 2016-11-14 | NULL | CTRL_MULTI01 | PENDING| john.doe | NULL | CTRL_MULTI01 | MULTIPLE | 19000207
16 | 2 | WAREHOUSE | DcMo | 6 | 2016-11-14 | NULL | CTRL_MULTI01 | PENDING| john.doe | NULL | CTRL_MULTI01 | MULTIPLE | 19000209
我想實現的是批量插入所有這些記錄到各自的目標位置和更新的數量,如果該位置前手有同樣的項目。在一個referenceNumber
但韓元
CREATE procedure updateTBLStock
@referenceNumber nvarchar(50),
@destinationLocation nvarchar(50)
as begin
--1) insert non existing items to a temporary table
INSERT INTO tblTempStockList
SELECT b.ProductID, a.ProductName, a.ProductCode, b.Quantity, a.UnitOfMeasure,
a.Provider, a.Category, a.ExpirationDate, b.DestinationLocation, b.ReferenceNumber
FROM tblStockMove b
inner join tblProducts_warehouse a on b.ProductCode = a.ProductCode
where b.ReferenceNumber = @referenceNumber
and NOT EXISTS (Select a.ProductCode from tblProducts_establishments a
where b.ProductCode = a.ProductCode
and a.Location = @destinationLocation
and b.ReferenceNumber = @referenceNumber
and b.MoveType = 'MULTIPLE')
--2) update items' quantity if they already exist in the destination location
UPDATE tblProducts_establishments
SET Quantity = a.Quantity + b.Quantity
from tblProducts_establishments a
left join tblStockMove b
on a.ProductCode = b.ProductCode
where b.ReferenceNumber = @referenceNumber
and b.MoveType = 'MULTIPLE'
and a.Location = @destinationLocation
and EXISTS (Select a.ProductCode from tblProducts_establishments a
where b.ProductCode = a.ProductCode and a.Location = @destinationLocation
and b.ReferenceNumber = @referenceNumber and b.MoveType = 'MULTIPLE')
--3) Insert the row from the temporary table to the main table
INSERT INTO tblProducts_establishments (ProductID, ProductName, ProductCode, Quantity, UnitOfMeasure, Date, Provider, Category, ExpirationDate, Status, Location)
SELECT ProductID, ProductName, ProductCode, Quantity, UnitOfMeasure, getdate(), Provider, Category, ExpirationDate, null, Location FROM tblTempStockList where ReferenceNumber = @referenceNumber and Location = @destinationLocation
--4) Empty the temporary table
DELETE FROM tblTempStockList where ReferenceNumber = @referenceNumber and Location = @destinationLocation
該查詢運行正常IF只有一個行(項):
因此,我做了這個存儲過程,而做了一系列的插入,更新和降如果有多個條目,則不能正確插入。
所以我嘗試使用遊標,但它會爲每個DestinationLocation插入重複的ProductID。
declare @destinationLocation nvarchar(50)
declare @referenceNumber nvarchar(50)
declare cur CURSOR LOCAL for
select DestinationLocation, ReferenceNumber from tblStockMove where ControlNumber = 'CTRL_MULTI01'
open cur
fetch next from cur into @destinationLocation, @referenceNumber
while @@FETCH_STATUS = 0 BEGIN
execute updateTBLStock @referenceNumber, @destinationLocation
fetch next from cur into @destinationLocation, @destinationLocation
END
close cur
deallocate cur
我的問題是,我怎樣才能成功地使用上面的光標調用存儲過程?如何從光標傳遞我的存儲過程需要的兩個變量?
你'CURSOR'是已經在做了。你目前的代碼有什麼問題? –
我的SP應該更新特定機構中現有產品的數量,例如,如果ProductID 1存在於DcMo中,並且tblStockMove有5,那麼它會將它們加在一起。然而,在使用遊標時,它會從tblStockMove插入相同的'ProductID',導致DcMo中出現重複的ID – Saudate