Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use transactions in SQLServer stored procedures

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report