--儲存過程加事務處理防止超出數量
use [lqscan]
go/**** object: storedprocedure [dbo].[upscanbarcode] script date: 2021/3/18 14:02:32 ****/
set ansi_nulls on
goset quoted_identifier on
goalter procedure [dbo].[upscanbarcode]
-- add the parameters for the stored procedure here
@orderno varchar(9),
@partcode nvarchar(50),
@barcode varchar(13),
@note nvarchar(500)
asdeclare @error int = -1 --事務中操作記錄 0:條碼存在; 1:訂單與物料不存在; 2:數量超出需求數量;13:條碼長度不對;200:資料提交成功;
,@i***ist int = 0
,@isok int = 0
,@barcodei***ist int = 0
begin
begin try
--開啟事務
begin tran
--判斷條碼是否是13位
if len(@barcode) <>13
begin
set @error = 13
raiserror('param errpr0',16,1)
end--判斷條碼是否已存在
select @barcodei***ist = count(1) from [lqscan].[dbo].[scanlog] where [barcode] = @barcode
if(@barcodei***ist > 0)
begin
set @error = 0
raiserror('param errpr0',16,1)
end--新增掃瞄資料
insert into [lqscan].[dbo].[scanlog]([barcode],[orderno],[note])values (@barcode,@orderno,@note)
--更新對應數量
update [lqscan].[dbo].[orderinfo] set [scanqty] = [scanqty] +1 where [orderno] = @orderno and [partcode] = @partcode
--判斷**訂單與物料編碼是否存在
select @i***ist = count(1) from [lqscan].[dbo].[orderinfo] where [orderno] = @orderno and [partcode] = @partcode
if( @i***ist < 1 )
begin
set @error = 1
raiserror('param errpr1',16,1)
end--判斷數量是否匹配完成
select @isok = count(1) from [lqscan].[dbo].[orderinfo] where [orderno] = @orderno and [partcode] = @partcode and [scanqty] > [needsqty]
if( @isok > 0)
begin
set @error = 2
raiserror('param errpr2',16,1)
endset @error = 200
commit tran
end try
begin catch
rollback tran
end catch
return @error
end
儲存過程調儲存過程的事務
直接上 是在包裡寫的儲存過程,要測試的話,要先寫宣告 宣告procedure test1 p retcode in out varchar2,p retinfo in out varchar2 procedure test2 p retcode in out varchar2,p retinfo i...
Sql 儲存過程 事務
alter procedure dbo usp pe delworklogbill id varchar 50 companycode varchar 50 asdeclare errno int set errno 0 begin tran 開始執行事務 delete from opeworklo...
事務和儲存過程
事務 同生共死 指訪問並可能更新資料庫中各種資料項的乙個程式執行單元 unit 也就是由多個sql語句組成,必須作為乙個整體執行 這些sql語句作為乙個整體一起向系統提交,要麼都執行 要麼都不執行 語法步驟 開始事務 begin transaction 事務提交 commit transaction...