Practical stored procedures for bank transfers and storage of sequential numbers, and stored procedures for bank transfers

Source: Internet
Author: User

Practical stored procedures for bank transfers and storage of sequential numbers, and stored procedures for bank transfers

Bank Transfer Stored Procedure

USE [BankInfor] GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ongoalter 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 'transfer-out account does not exist or there is no deposit in the account 'Return; end if @ totalDeposit <@ amount begin rollback; print 'the balance is insufficient, 'Return; end update Account set total = total-@ amount where AccountNum = @ outAccount; update Account set total = total + @ amount where AccountNum = @ inAccount; print 'transfer successful! 'Commit; end;

Storage Process for serial number generation

If exists (select 1 from sysobjects where id = OBJECT_ID ('getserialno') and xtype = 'P') drop proc GetSerialNogoCreate procedure [dbo]. [GetSerialNo] (@ sCode varchar (50) as begin Declare @ sValue varchar (16), @ dToday datetime, @ sQZ varchar (50) -- This indicates the prefix Begin Tran Begin Try -- lock this record. Many people use lock to lock the record. At the beginning, only one update statement can be executed. -- in the same thing, after the update statement is executed, the lock Update SerialNo set sValue = sValue where sCode = @ sCode Select @ sValue = sValue From SerialNo where sCode = @ sCode Select @ sQZ = sQZ From SerialNo where sCode = @ sCode -- there is no record in the factor table, insert initial value If @ sValue is null Begin Select @ sValue = convert (bigint, convert (varchar (6), getdate (), 12) + '123 ') update SerialNo set sValue = @ sValue where sCode = @ sCode end else Begin -- Select @ dToday = substring (@ sValue,) is not recorded in the factor table -- if the date is equal, add 1 If @ dToday = convert (varchar (6), getdate (), 12) Select @ sValue = convert (varchar (16), (convert (bigint, @ sValue) + 1) else -- if the date is not equal, assign a value to the date first. The serial number starts from 1 Select @ sValue = convert (bigint, convert (varchar (6), getdate (), 12) + '123 ') 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 SerialNoselect convert (varchar (6 ), getdate (), 12) + '123'

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.