在儲存過程中運用事務

2021-08-31 14:58:23 字數 3049 閱讀 2086

在儲存過程中運用事務

create database study

use study

create table peopleinfor  --使用者資訊表

(id int primary key identity(1001,1),--主鍵自動增長

name1 nvarchar(30),--使用者名稱

ymoney   money default(0)--卡上剩餘金額

)create table peopleinfor_mx  --使用者取款明細

(mx_id int primary key identity(100001,1), --主鍵自動增長

mx_money money ,  --取款或付款金額

mx_type nvarchar(20)  check (mx_type='取錢' or mx_type ='存錢'),--代表此條資料是 存錢,還是 取錢

peopleid int  foreign key (peopleid)references peopleinfor(id) not null

)--建立乙個基本的儲存過程

create procedure pro_insert(@name nvarchar(30),@money money)as

begin 

insert into peopleinfor(name1,ymoney)values(@name,@money)

end--執行 儲存過程。

exec pro_insert '趙剛',20000

exec pro_insert '王五',30000

--錯誤的例子模仿銀行轉賬

create procedure pro_zhuangzhang 

@name_jin nvarchar(30),--轉進賬戶名稱

@name_chu nvarchar(30), --轉出帳戶名稱

@money money --轉入錢數)

asbegin tran  --開發事務

declare @ymoney money --賬上餘額

declare @chuid int --出款賬號的id

declare @jinid int --進款賬號的id

select  @ymoney=ymoney from peopleinfor where name1=@name_chu --獲取賬上餘額

if(@money<=@ymoney)--判斷 賬號餘額 是否 大於等於 轉款餘額

begin

update peopleinfor set ymoney=ymoney-@money where name1=@name_chu  -- 從 出帳 帳戶中減去 轉出金額

select @chuid=id from  peopleinfor where name1=@name_chu     -- 獲得出帳id

insert into peopleinfor_mx (mx_money,mx_type,peopleid)values(@money,'取錢',@chuid) --把轉出金額 插入明細 表中並標明是 取出的錢

update peopleinfor set ymoney=ymoney+@money where name1=@name_jin-- 在 進帳 帳戶中加上 轉出金額

select @chuid=id from  peopleinfor where name1=@name_jin-- -- 獲得出帳id

insert into peopleinfor_mx (mx_money,mx_type,peopleid)values(@money,'存錢',@jinid)

-- 此時 @jinid 為空時 ,不能插入此列。故軟體報錯。

if @@error<>0

begin

rollback tran 

end 

endcommit tran --執行事務

go--對的例子 模仿銀行轉賬

create procedure pro_zhuangzhang 

@name_jin nvarchar(30),--轉進賬戶名稱

@name_chu nvarchar(30), --轉出帳戶名稱

@money money --轉入錢數)

asbegin tran  --開發事務

declare @ymoney money 

declare @chuid int 

declare @jinid int 

select  @ymoney=ymoney from peopleinfor where name1=@name_chu

if(@money<@ymoney)

begin

update peopleinfor set ymoney=ymoney-@money where name1=@name_chu

select @chuid=id from  peopleinfor where name1=@name_chu

insert into peopleinfor_mx (mx_money,mx_type,peopleid)values(@money,'取錢',@chuid)

update peopleinfor set ymoney=ymoney+@money where name1=@name_jin

select @jinid=id from  peopleinfor where name1=@name_jin

insert into peopleinfor_mx (mx_money,mx_type,peopleid)values(@money,'存錢',@jinid)

if @@error<>0

begin

rollback tran 

end 

endcommit tran --執行事務

goselect * from peopleinfor

select * from peopleinfor_mx

delete from peopleinfor where id=1003

exec pro_zhuangzhang '趙剛','王五',20000

'趙剛',20000

'王五',30000

儲存過程中使用事務

create procedure updatewanjun username nvarchar 500 userpassword nvarchar 500 returnval int output as set xact abort on begin transaction t update adm...

在SQL Server儲存過程中使用事務及返回值

1 create procedure testtran23 as45declare userid int6 7set nocount on8 9begin tran adduser 1011 insert into testtable username,password,email values m...

mysql儲存過程中使用事務

mysql儲存過程中使用事務 1 drop procedure ifexists test sp1 2create procedure test sp1 3begin 4declare t error integer default 0 5declare continue handler for s...