存储过程生成流水号
2012-02-07 10:03
387 查看
格式如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
/****** 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
相关文章推荐
- SQL存储过程生成流水号
- 一个用来生成流水号的存储过程
- 实用的银行转账存储过程和流水号生成存储过程
- 存储过程生成流水号----灵活版
- SLQ生成订单号 序列码 流水号的存储过程
- SQLserver 存储过程生成任意进制/顺序流水号
- 一个生成订单流水号的存储过程
- SQL存储过程生成流水号
- SQL Server 存储过程生成流水号
- 根据日期+顺序号生成流水号的存储过程
- 一个用来生成流水号的存储过程
- 一个MSSql的存储过程---生成编码,形如HEAD20060512000001;(头+日期+6位递增整数)后面为六位,每新的一年编号重新归0
- mysql 生成完整的存储过程定义语句
- 转:生成sql server2000对象创建脚本的存储过程
- 生成表中数据的sql语法的存储过程
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- T-SQL 生成的公历年历 (写在了存储过程中了)
- 自动生成INSERT语句的SQL存储过程
- 也谈代码生成-----(存储过程生成工具基本原理)