如果不存在则插入数据(来自 2 个表),否则更新
Insert Data if not exists (from 2 tables) and Update otherwise
美好的一天。我有 3 个表:
tblWarehouseProducts:
ProductID
ProductName
ProductCode
Quantity
tblBranchProducts:
ProductID
ProductCode
ProductCode
Quantity
Location
tblStockMoves:
ProductID
DestinationLocation
Quantity
ReferenceNumber
基本上,流程是分支 X 向仓库 Y 请求产品。然后仓库 Y 创建请求订单(称为 库存移动)并将请求存储在 tblStockMove.
对于这种情况,我们有参考编号 XYZ 的股票变动:
REFERENCE NO. | PRODUCT ID | DESTINATION | QTY |
XYZ | 1 | BRANCH Y | 5 |
XYZ | 2 | BRANCH Y | 6 |
(其中 ProductID 1 是可口可乐,ProductID 2 是百事可乐。)
另一方面,X 分店有此产品库存:
PRODUCT ID | PRODUCT NAME | PRODUCT CODE | QUANTITY | LOCATION |
1 | COKE | ABC123 | 6 | Branch X |
我目前正在尝试检查 tblBranchProducts 中是否存在来自 tblStockMoves 的项目。
如果产品 1 存在,它会将 tblStockMoves 中的数量添加到 tblBranchProducts 中的当前数量。产品 2 将添加为新条目,因为它是新项目。
我在下面使用这个查询,但到目前为止,它所做的只是更新 ProductID 1 的库存,同时忽略(不插入)Product ID 2。
IF EXISTS (select ProductID, Location
from tblBranchProducts a
where Location = 'Branch X'
and a.ProductID in (select b.ProductID
from tblStockMoves b
where b.ReferenceNumber = 'XYZ'
and b.DestinationLocation = 'Branch X'))
BEGIN
UPDATE tblBranchProducts
SET Quantity = a.Quantity + b.Quantity
FROM tblBranchProducts a
INNER JOIN tblStockMoves b ON a.ProductID = b.ProductID
WHERE
b.ReferenceNumber = 'XYZ'
AND b.DestinationLocation = 'Branch X'
END
ELSE
BEGIN
INSERT INTO tblBranchProducts (ProductID, ProductName, ProductCode, Quantity, Location)
SELECT
b.ProductID, a.ProductName, a.ProductCode, b.Quantity, b.DestinationLocation
FROM
tblStockMoves b
INNER JOIN
tblWarehouseProducts a ON b.ProductID = a.ProductID
WHERE
b.ReferenceNumber = 'XYZ'
AND b.DestinationLocation = 'Branch X'
产品名称和产品代码等其他详细信息从 tblWarehouseProducts 中提取,然后插入到 tblBranchProducts 中。
谁能告诉我为什么我的查询只更新产品 1 的现有库存而不插入产品 2?
非常感谢您的回答!
您可以动态地为所有没有IF
的产品做,只需添加所需的条件:
/*will insert all the unmatched products*/
INSERT INTO tblBranchProducts (ProductID, ProductName, ProductCode, Quantity, Location)
SELECT b.ProductID, a.ProductName, a.ProductCode, b.Quantity, b.DestinationLocation
FROM tblStockMoves b
inner join tblWarehouseProducts a on b.ProductID = a.ProductID
LEFT JOIN tblBranchProducts c ON(a.productid = b.productid)
where c.productid is null
并且:
/*will update all the matching products*/
update tblBranchProducts a
INNER join tblStockMoves b on a.productid = b.productid
set a.quantity= b.qty
美好的一天。我有 3 个表:
tblWarehouseProducts:
ProductID
ProductName
ProductCode
Quantity
tblBranchProducts:
ProductID
ProductCode
ProductCode
Quantity
Location
tblStockMoves:
ProductID
DestinationLocation
Quantity
ReferenceNumber
基本上,流程是分支 X 向仓库 Y 请求产品。然后仓库 Y 创建请求订单(称为 库存移动)并将请求存储在 tblStockMove.
对于这种情况,我们有参考编号 XYZ 的股票变动:
REFERENCE NO. | PRODUCT ID | DESTINATION | QTY |
XYZ | 1 | BRANCH Y | 5 |
XYZ | 2 | BRANCH Y | 6 |
(其中 ProductID 1 是可口可乐,ProductID 2 是百事可乐。)
另一方面,X 分店有此产品库存:
PRODUCT ID | PRODUCT NAME | PRODUCT CODE | QUANTITY | LOCATION |
1 | COKE | ABC123 | 6 | Branch X |
我目前正在尝试检查 tblBranchProducts 中是否存在来自 tblStockMoves 的项目。
如果产品 1 存在,它会将 tblStockMoves 中的数量添加到 tblBranchProducts 中的当前数量。产品 2 将添加为新条目,因为它是新项目。
我在下面使用这个查询,但到目前为止,它所做的只是更新 ProductID 1 的库存,同时忽略(不插入)Product ID 2。
IF EXISTS (select ProductID, Location
from tblBranchProducts a
where Location = 'Branch X'
and a.ProductID in (select b.ProductID
from tblStockMoves b
where b.ReferenceNumber = 'XYZ'
and b.DestinationLocation = 'Branch X'))
BEGIN
UPDATE tblBranchProducts
SET Quantity = a.Quantity + b.Quantity
FROM tblBranchProducts a
INNER JOIN tblStockMoves b ON a.ProductID = b.ProductID
WHERE
b.ReferenceNumber = 'XYZ'
AND b.DestinationLocation = 'Branch X'
END
ELSE
BEGIN
INSERT INTO tblBranchProducts (ProductID, ProductName, ProductCode, Quantity, Location)
SELECT
b.ProductID, a.ProductName, a.ProductCode, b.Quantity, b.DestinationLocation
FROM
tblStockMoves b
INNER JOIN
tblWarehouseProducts a ON b.ProductID = a.ProductID
WHERE
b.ReferenceNumber = 'XYZ'
AND b.DestinationLocation = 'Branch X'
产品名称和产品代码等其他详细信息从 tblWarehouseProducts 中提取,然后插入到 tblBranchProducts 中。
谁能告诉我为什么我的查询只更新产品 1 的现有库存而不插入产品 2?
非常感谢您的回答!
您可以动态地为所有没有IF
的产品做,只需添加所需的条件:
/*will insert all the unmatched products*/
INSERT INTO tblBranchProducts (ProductID, ProductName, ProductCode, Quantity, Location)
SELECT b.ProductID, a.ProductName, a.ProductCode, b.Quantity, b.DestinationLocation
FROM tblStockMoves b
inner join tblWarehouseProducts a on b.ProductID = a.ProductID
LEFT JOIN tblBranchProducts c ON(a.productid = b.productid)
where c.productid is null
并且:
/*will update all the matching products*/
update tblBranchProducts a
INNER join tblStockMoves b on a.productid = b.productid
set a.quantity= b.qty