我有两张桌子
将数据插入ProductMst
时,代码只插入一个值,但PriceMst
中有条件如果价格> 300 它将插入1千克,750克,500克,250克各自计算根据重量的价格为1千克,750克,500克,250克如果价格< 300 它应该只插入一次。
并在PriceMst
中生成一些以Productcode
为主的ID。 FP-001和生成 PriceCode就像这个FP-001-01。
我的表格描述是
tblProduct
ProductCode ProductName ProductPrize ProductSizeID
------------------------------------------------------
FP-001 ABC 200.00 4
FP-002 PQW 500.00 3
FP-003 ASD 1200.00 4
tblPriceMST
ProductCode ProductPriceID ProductPrize ProductSize ProductUnit
----------------------------------------------------------------------
FP-001 FP-001-05 200.00 1 KG
FP-002 FP-002-01 500.00 1 KG
FP-002 FP-002-02 375.00 750 GMS
FP-002 FP-002-03 250.00 500 GMS
FP-002 FP-002-04 125.00 250 GMS
FP-003 FP-003-01 1200.00 1 KG
FP-003 FP-003-02 900.00 750 GMS
FP-003 FP-003-03 600.00 500 GMS
FP-003 FP-003-04 300.00 250 GMS
请指导我如何在单个查询中插入两个表。
此查询将用于我的存储过程。
答案 0 :(得分:1)
你需要做一些事情来完成这项工作。对于存储过程的第一部分,您需要插入到tblProduct表中,然后将新的ProductCode存储在变量中。我假设ProductCode是通过触发器或计算列生成的。如果您没有自动生成,最好的方法是creating a computed column。
下面使用ProductName和ProductPrice作为参数创建过程,将它们插入到tblProduct中,并将新的ProductCode存储到@NewProductCode临时表中。
CREATE PROCEDURE dbo.InsertProduct @ProductName nvarchar(255), @ProductPrice decimal(19,4)
AS
DECLARE @NewProductCodeTempTable table (ID int)
INSERT INTO tblProduct (ProductName, ProductPrize)
OUTPUT INTO INSERTED.ProductCode @NewProductCode
VALUES (@ProductName, @ProductPrice)
接下来,您将使用IF / ELSE语句来确定是否需要在tblPriceMST表中插入一条或4条记录。
DECLARE @NewProductCode = SELECT ID FROM @NewProductCodeTempTable
IF (@ProductPrice < 300)
INSERT INTO tblPriceMST (ProductCode, ProductPriceID, ProductPrize, ProductSize, ProductUnit)
VALUES (@NewProductCode, @NewProductCode + '-01', @ProductPrice, 1, 'KG')
ELSE
BEGIN
INSERT INTO tblPriceMST (ProductCode, ProductPriceID, ProductPrize, ProductSize, ProductUnit)
VALUES (@NewProductCode, @NewProductCode + '-01', @ProductPrice, 1, 'KG')
INSERT INTO tblPriceMST (ProductCode, ProductPriceID, ProductPrize, ProductSize, ProductUnit)
VALUES (@NewProductCode, @NewProductCode + '-02', @ProductPrice*.75, 750, 'GMS')
INSERT INTO tblPriceMST (ProductCode, ProductPriceID, ProductPrize, ProductSize, ProductUnit)
VALUES (@NewProductCode, @NewProductCode + '-03', @ProductPrice*.5, 500, 'GMS')
INSERT INTO tblPriceMST (ProductCode, ProductPriceID, ProductPrize, ProductSize, ProductUnit)
VALUES (@NewProductCode, @NewProductCode + '-04', @ProductPrice*.25, 250, 'GMS')
END
GO
如果价格> 1,我已经推断出您为不同产品重量插入记录的规则。 300.但是如果你有更多规则,你可以在ELSE块中添加额外的IF / THEN或CASE语句。
如果你简化你的桌面设计但是在不知道你的所有要求的情况下很难说它应该是什么样子,那么可能有一种更简单的方法。
答案 1 :(得分:0)
首先,您的ProductSizeID似乎被忽略了 - 这是预期的行为吗?它没有在tblPriceMST中使用......
然而,如果你总是只需要单位1 KG的奖品ID 01所有价格&lt;所有价格&gt; = 300,总是奖金ID 2 - 4为1 KG到250 G,您可以简单地使用UNION ALL:
SELECT ProductCode, ProductCode+'-01' AS ProductPriceID, ProductPrize, 1 ProductSize, 'KG' ProductUnit
FROM tblProduct
WHERE ProductPrize < 300
UNION ALL
SELECT ProductCode, ProductCode+'-01' AS ProductPriceID, ProductPrize, 1 ProductSize, 'KG' ProductUnit
FROM tblProduct
WHERE ProductPrize >= 300
UNION ALL
SELECT ProductCode, ProductCode+'-02' AS ProductPriceID, ProductPrize*0.75, 750 ProductSize, 'G' ProductUnit
FROM tblProduct
WHERE ProductPrize >= 300
UNION ALL
SELECT ProductCode, ProductCode+'-03' AS ProductPriceID, ProductPrize*0.5, 500 ProductSize, 'G' ProductUnit
FROM tblProduct
WHERE ProductPrize >= 300
UNION ALL
SELECT ProductCode, ProductCode+'-04' AS ProductPriceID, ProductPrize*0.25, 250 ProductSize, 'G' ProductUnit
FROM tblProduct
WHERE ProductPrize >= 300
ORDER BY 1, 2
有关详细信息,请参阅SQLFiddle:http://sqlfiddle.com/#!18/0910e/7