Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use transactions in SQLServer stored procedures

Shulou Source: shulou.com Published: 2022-05-31 18:58:27 10月06日 Update

SQLServer stored procedures how to use transactions, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The specific code is as follows

Create proc usp_Stock@GoodsId int,@ Number int,@ StockPrice money, @ SupplierId int,@ EmpId int,@ StockUnit varchar (50), @ StockDate datetime, @ TotalMoney money, @ ActMoney money, @ baseId int,@Description nvarchar (255) as declare @ error int = 0-- error record of operation in transaction-- Open transaction begin transaction-- add insert into StockInfo values (@ GoodsId, @ Number, @ StockPrice, @ SupplierId, @ EmpId) to purchase information. @ StockUnit, @ StockDate, @ TotalMoney, @ ActMoney,DEFAULT,@Description, @ baseId) set @ error+=@@ERROR-- record the possible error number-- the identification column for obtaining the current purchase information-- determine whether the current item has a purchase record if exists (select * from dbo.InventoryInfo where goodid=@GoodsId)-- indicate that the record exists. Directly modify the inventory quantity begin update dbo.InventoryInfo set GNumber=GNumber+@Number,TotalMoney+=@TotalMoney where goodid=@GoodsId set @ error+=@@ERROR-- record the possible error number end else-- this item has never been purchased. Then you should add the new existence information begin declare @ GWarningNum int-- the early warning quantity of this item-- to get the early warning quantity set @ GWarningNum= (select WaringNum from dbo.GoodsInfo where GId=@GoodsId) insert into dbo.InventoryInfo values (@ GoodsId,@Number,@baseId,@GWarningNum,@TotalMoney,' first purchase). Default) set @ error+=@@ERROR-record the possible error number end-- to determine the commit of the transaction or roll back if (@ error0) begin rollback transaction return-1-set the error identification of the operation result endelse begin commit transaction return 1-is it helpful for you to read the above after reading the endgo that the operation is successful? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Errors transactions information goods quantity identification help early warning process storage clarity success code content this inventory articles novice more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Shulou Information Apple OPPO Reno