實現銀行轉賬操作,先對表進行分析 賬戶信息表是少不了的,其次還需要一個記錄轉賬操作的轉賬記錄表
eg:
create database RiKao1019 --創建數據庫
use RiKao1019 --使用數據庫
--創建賬戶信息表
create table Infor
(
Id int primary key identity, --主鍵自增Id
Number varchar(19), --銀行卡號
Pass varchar(20), --用戶密碼
Balance decimal --余額
)
--創建轉賬記錄表
create table TransferInfor
(
Tid int primary key identity, --主鍵自增Id
Ttype varchar(4), --轉賬類型,判斷是入賬還是出賬
Operational decimal, --轉賬金額
Ttime datetime --轉賬時間
)
--創建轉賬存儲過程
if OBJECT_ID('p_transfer','p') is not null --判斷該存儲過程是否存在
drop proc p_transfer
go
create proc p_transfer
@out_Number varchar(20),--轉出賬號
@in_Number varchar(20), --轉入賬號
@Balance decimal(18,2), --賬戶金額
@Balance1 decimal(18,2), --轉賬金額
@Ttime datetime --轉賬時間
as
begin try
select @Balance=Balance from Infor where Number=@out_Number --查詢轉出的金額
if @Balance>=@Balance1 --判斷賬戶余額是否大於轉賬金額 是轉賬成功 否 轉賬失敗
begin
update Infor set Balance=Balance-@Balance1 where Number=@out_Number --付款賬戶減少了轉賬金額
update Infor set Balance=Balance+@Balance1 where Number=@in_Number --收款賬戶增加了轉賬金額
insert into TransferInfor values ('轉出',@Balance1,@Ttime) --對數據記錄表執行操作
print'轉賬成功'
end
else
begin
print'余額不足'
end
end try
begin catch
if(@@trancount>0) --代表提交事務的數量
declare @message varchar(50)
set @message=(select ERROR_MESSAGE() as message)
print @message
rollback tran --回滾事務
end catch
if(@@TRANCOUNT>0)
commit tran --提交事務
因為實際業務中存在各種各樣的條件,比如跨行操作 所以我做的例子只限於很基本 的用存儲過程加事務完成銀行轉賬的一個步驟,給大家提供一個思路,具體的需要根據實際情況的不同進行添加修改條件