實(shí)用的銀行轉(zhuǎn)賬存儲(chǔ)過(guò)程和流水號(hào)生成存儲(chǔ)過(guò)程
更新時(shí)間:2015年09月28日 11:23:15 作者:只喝牛奶的殺手
本篇文章給大家分享銀行轉(zhuǎn)賬存儲(chǔ)過(guò)程和流水號(hào)生成存儲(chǔ)過(guò)程,感興趣的朋友一起看看吧
銀行轉(zhuǎn)賬存儲(chǔ)過(guò)程
USE [BankInfor] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Transfer](@inAccount int,@outAccount int,@amount float) as declare @totalDeposit float; begin select @totalDeposit=total from Account where AccountNum=@outAccount; if @totalDeposit is null begin rollback; print'轉(zhuǎn)出賬戶不存在或賬戶中沒(méi)有存款' return; end if @totalDeposit<@amount begin rollback; print'余額不足,不能操作' return; end update Account set total=total-@amount where AccountNum=@outAccount; update Account set total=total+@amount where AccountNum=@inAccount; print'轉(zhuǎn)賬成功!' commit; end;
流水號(hào)生成存儲(chǔ)過(guò)程
if exists(select 1 from sysobjects where id=OBJECT_ID('GetSerialNo') and xtype='p')
drop proc GetSerialNo
go
Create procedure [dbo].[GetSerialNo]
(
@sCode varchar(50)
)
as
begin
Declare @sValue varchar(16),@dToday datetime,@sQZ varchar(50) --這個(gè)代表前綴
Begin Tran
Begin Try
-- 鎖定該條記錄,好多人用lock去鎖,起始這里只要執(zhí)行一句update就可以了
--在同一個(gè)事物中,執(zhí)行了update語(yǔ)句之后就會(huì)啟動(dòng)鎖
Update SerialNo set sValue=sValue where sCode=@sCode
Select @sValue = sValue From SerialNo where sCode=@sCode
Select @sQZ = sQZ From SerialNo where sCode=@sCode
-- 因子表中沒(méi)有記錄,插入初始值
If @sValue is null
Begin
Select @sValue = convert(bigint, convert(varchar(6), getdate(), 12) + '000001')
Update SerialNo set sValue=@sValue where sCode=@sCode
end else
Begin --因子表中沒(méi)有記錄
Select @dToday = substring(@sValue,1,6)
--如果日期相等,則加1
If @dToday = convert(varchar(6), getdate(), 12)
Select @sValue = convert(varchar(16), (convert(bigint, @sValue) + 1))
else --如果日期不相等,則先賦值日期,流水號(hào)從1開(kāi)始
Select @sValue = convert(bigint, convert(varchar(6), getdate(), 12) +'000001')
Update SerialNo set sValue =@sValue where sCode=@sCode
End
Select result = @sQZ+@sValue
Commit Tran
End Try
Begin Catch
Rollback Tran
Select result = 'Error'
End Catch
end
select*from SerialNo
select convert(varchar(6), getdate(), 12)+'000001'
您可能感興趣的文章:
- Android 高仿微信轉(zhuǎn)賬金錢輸入框規(guī)則
- python 實(shí)現(xiàn)網(wǎng)上商城,轉(zhuǎn)賬,存取款等功能的信用卡系統(tǒng)
- Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)
- Python3連接MySQL(pymysql)模擬轉(zhuǎn)賬實(shí)現(xiàn)代碼
- 基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的銀行轉(zhuǎn)賬操作
- PHP+Mysql基于事務(wù)處理實(shí)現(xiàn)轉(zhuǎn)賬功能的方法
- php利用事務(wù)處理轉(zhuǎn)賬問(wèn)題
- php+Mysqli利用事務(wù)處理轉(zhuǎn)賬問(wèn)題實(shí)例
- php+mysqli事務(wù)控制實(shí)現(xiàn)銀行轉(zhuǎn)賬實(shí)例
- 轉(zhuǎn)賬支票、現(xiàn)金支票日期大寫(xiě)對(duì)照表(數(shù)字大寫(xiě))
- 微信開(kāi)發(fā)--企業(yè)轉(zhuǎn)賬到用戶
相關(guān)文章
分頁(yè)存儲(chǔ)過(guò)程(用存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)據(jù)庫(kù)的分頁(yè)代碼)
用存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)據(jù)庫(kù)的分頁(yè)代碼,加快頁(yè)面執(zhí)行速度。具體的大家可以測(cè)試下。2010-06-06
SQL?Server?Agent?服務(wù)啟動(dòng)后又停止問(wèn)題
這篇文章主要介紹了SQL?Server?Agent?服務(wù)啟動(dòng)后又停止問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
SQL點(diǎn)滴24 監(jiān)測(cè)表的變化
在網(wǎng)上看到一篇關(guān)于監(jiān)測(cè)表中的插入,更新,刪除的方法,使用觸發(fā)器實(shí)現(xiàn)的,很有價(jià)值。2011-09-09
Linq to SQL 插入數(shù)據(jù)時(shí)的一個(gè)問(wèn)題
今天用LinqtoSql插入數(shù)據(jù),總是插入錯(cuò)誤,說(shuō)某個(gè)主鍵字段不能為空,我檢查了半天感覺(jué)主鍵字段沒(méi)有賦空值啊,實(shí)在是郁悶。 要插入數(shù)據(jù)的表結(jié)構(gòu)是2009-08-08
SQL?Server?查詢?cè)O(shè)置??LIKE/DISTINCT/HAVING/排序詳解
本文介紹了在SQLServer中使用LIKE、DISTINCT、HAVING等條件語(yǔ)句進(jìn)行復(fù)雜查詢的方法,包括模糊查詢、去重查詢、分組過(guò)濾以及排序等技巧,感興趣的朋友跟隨小編一起看看吧2025-01-01
獲取MSSQL數(shù)據(jù)字典的SQL語(yǔ)句
獲取MSSQL數(shù)據(jù)字典的實(shí)現(xiàn)方法。2009-04-04

