私は、ユーザから2つのパラメータ、すなわちreferenceNumber
とdestinationLocation
を取得するストアドプロシージャを持っています。ここで、referenceNumber
は行の一意の識別子であり、destinationLocation
は要求されたアイテムが配信される場所です。その場所は、手の前に同じアイテムを持っていた場合各行ごとにストアドプロシージャが返されますか?
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
がそれぞれの目的地の場所にこれらすべてのレコードを挿入し、数量を更新:下表tblStockMove
を参照してください。 1 referenceNumber
しかしウォンでのみ一列(項目)がある IF
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
このクエリは正常に動作:このよう
は、私は、挿入、更新、およびドロップの一連の処理を行い、このストアドプロシージャを作っ複数のエントリがある場合、正しく挿入しないでください。
私はカーソルを使用しようとしましたが、DestinationLocationごとに重複したProductIDを挿入しました。
私の質問は、上記のカーソルを使用してストアドプロシージャを正常に呼び出す方法です。ストアドプロシージャに必要なカーソルから2つの変数を渡すにはどうすればよいですか?
AS
DECLAREの@StockType以下のように自分のパラメータを渡します。あなたの現在のコードで何が間違っていますか? –
私のSPは、特定の施設内の既存製品の数量を更新することになっています。例えば、「ProductID 1」がDcMoに存在し、tblStockMoveが5である場合、それらは一緒に追加されます。ただし、カーソルを使用すると、tblStockMoveから同じ「ProductID」が挿入され、DcMoにIDが重複します – Saudate