存储过程生成流水号

格式如A20110915001,第1位写死A,2-5位为年,6-7位为月,8-9位为日,最后三位为流水号.


/****** Object:  StoredProcedure [dbo].[usp_SaleRetrun_CreateAutoApplyBatchId]    Script Date: 02/07/2012 09:55:40 ******/
SET ANSI_NULLS ON
GO


SET QUOTED_IDENTIFIER OFF
GO

CREATE  PROCEDURE [dbo].[usp_CreateAutoBatchId]


AS
BEGIN
SET NOCOUNT ON;
declare @oldValue varchar(12)
declare @newValue varchar(12)
declare @newCode varchar(3)
declare @oldCode varchar(3)
declare @oldYearMonthDay varchar(9)
declare @newYearMonthDay varchar(9)

set @newYearMonthDay = CONVERT(varchar(8), GETDATE(), 112)
set @newYearMonthDay ='R'+@newYearMonthDay
select @oldValue = pValue from Parameter with (rowlock) where pName='Program_Batch_Id' and pActive='Y'
set @oldYearMonthDay = substring(@oldValue,1,9)
set @oldCode = substring(@oldValue,10,3)


if @newYearMonthDay = @oldYearMonthDay --如果新的年月日编号与旧的相同
begin
-- 旧序号+1
set @newCode = cast(cast(@oldCode as int)+1 as varchar(3))
while len(@newCode)<3
begin
set @newCode = '0' + @newCode
end
set @newValue = @oldYearMonthDay + @newCode
end
else --采用新的年月编号,序号从"001"开始
begin
set @newCode = '001'
set @newValue = @newYearMonthDay + @newCode
end

    Begin transaction           --开始事务
update Parameter with (rowlock) set pValue = @newValue  where pName='Program_Batch_Id' and pActive='Y'
if @@ERROR > 0
ROLLBACK
Else
COMMIT

END

GO




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值