我想创建相关的存储过程处理购买。将数据插入itemPurchase表并更新Item表。
CREATE PROC [dbo].[ItemPurchaseProc]
--@id int
@ItemPrice float,
@Itemqty int
AS
BEGIN
insert into ItemPurchase(ItemPrice, ItemQty, ItemTotal)
values (@ItemPrice, @Itemqty, @ItemPrice * @Itemqty)
Update Item
Set StkCount =- @Itemqty
END
这是我写的。以下是两个表。告诉我如何创建此过程。提前谢谢!
CREATE TABLE dbo.Item
(
ItemCode AS 'IT' + RIGHT('000' + CAST(id as varchar(10)), 3) PERSISTED,
id int NOT NULL IDENTITY (1,1),
CONSTRAINT PK_Item PRIMARY KEY CLUSTERED (ItemCode),
Item_Desc varchar(30),
Quantity int,
MinStkLvl int,
SalesPrice float,
StkCount int
);
CREATE TABLE dbo.ItemPurchase
(
InvCode AS 'IN' + RIGHT('000' + CAST(id as varchar(10)), 3) PERSISTED,
CONSTRAINT FK_InvCode1
FOREIGN KEY (InvCode) REFERENCES Invoice(InvCode)
ON DELETE CASCADE,
id int NOT NULL IDENTITY (1,1),
ItemCode AS 'IT' + RIGHT('000' + CAST(id as varchar(10)), 3) PERSISTED,
CONSTRAINT FK_ItemCode1
FOREIGN KEY (ItemCode) REFERENCES Item(ItemCode)
ON DELETE CASCADE,
ItemPrice float,
Itemqty int,
ItemTotal float
);
答案 0 :(得分:0)
不清楚你需要什么。
但是,表上的2个操作需要处于事务中。
- 应在此处添加更新MinStkLvl的业务逻辑。也许触发一个触发器(?)或调用另一个存储过程...
CREATE PROCEDURE [dbo].[HandlePurchase]
(
@itemId int,
@qty int,
@double price
)
AS
BEGIN TRANSACTION;
BEGIN TRY
- 在此处将更新写入表格 - 在更新项目表之前,再次检查最小库存水平 - 您的业务逻辑
COMMIT
END TRY
BEGIN CATCH
ROLLBACK;
PRINT ERROR_MESSAGE();
END CATCH