自动编号的存储过程 (转)

本文介绍了一个用于自动编号的存储过程,该过程可以根据不同的日期粒度(年、月、日)生成带有日期前缀的流水号,并支持不同业务类型。通过更新数据库中的流水号表来实现每次调用时流水号的递增。
自动编号的存储过程 (转)[@more@]

CREATE PROCEDURE Get_BH @cL_MC char(20),@nL_Init Int AS
begin
  Declare @nL_CD Numeric(2,0),@cL_LSH char(20),@cL_LX Char(1),@nL_CDT Int,@nL_CDM int
  Declare LSHB_cursor Cursor For select Cd,LSH,Lx From LSHB Where Mailto:MC=@cL_MC">MC=@cL_MC
  Open LSHB_cursor
  Fetch Next From LSHB_Cursor into @nL_CD,@cL_LSH,@cL_LX
  If  @@FETCH_STATUS = 0
  begin
  Select @nL_CDM=Len(RTrim(Convert(Char(10),@nL_Init)))
  If  @cL_LX='1'
  begin  -- 前面四位为年 '2000'
  Select @nL_CDT=@nL_CD-4
  If  left(@cL_LSH,4)=convert(char(4),getdate(),102)  /*  年份相等,序号加 1  */
  begin
  Select @nL_CDM=Len(RTrim(Convert(Char(20),Convert(int,Right(@cL_LSH,16))+1)))
  Update LSHB set LSH=Left(@cL_LSH,4)+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(20),Convert(int,Right(@cL_LSH,16))+1))
  where MC=@cL_MC
  End
  else
  If @nL_Init>1
  Update LSHB set LSH=replace(Convert(Char(4),getdate(),102),'.','')+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(10),@nL_Init))
  where MC=@cL_MC
  Else
  Update LSHB set LSH=replace(Convert(Char(4),getdate(),102),'.','')+Replicate('0',@nL_CDT-1)+'1'
  where MC=@cL_MC
  End
  If  @cL_LX='2'
  begin  -- 前面六位为年月 '200008'
  Select @nL_CDT=@nL_CD-6
  If  left(@cL_LSH,4)=convert(char(4),getdate(),102) AND Convert(int,substring(@cL_LSH,5,2))=DATEPART(month,getdate())
  /*  年月相等,序号加 1  */
  begin
  Select @nL_CDM=Len(RTrim(Convert(Char(20),Convert(int,Right(@cL_LSH,14))+1)))
  Update LSHB set LSH=Left(@cL_LSH,6)+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(20),Convert(int,Right(@cL_LSH,14))+1))
  where MC=@cL_MC
  End
  else
  If @nL_Init>1
  Update LSHB set LSH=replace(Convert(Char(7),getdate(),102),'.','')+Replicate('0',@nL_CDT-@nL_CDM)+RTrim(Convert(Char(10),@nL_Init))
  where MC=@cL_MC
  Else
  Update LSHB set LSH=replace(Convert(Char(7),getdate(),102),'.','')+Replicate('0',@nL_CDT-1)+'1'
  where MC=@cL_MC
  End
  If  @cL_LX='3'
  begin  -- 前面八位为年月日 '20000910'
  Select @nL_CDT=@nL_CD-8 
  If  left(@cL_LSH,4)=convert(char(4),getdate(),102) AND Convert(int,substring(@cL_LSH,5,2))=DATEPART(month,getdate())
  AND Convert(Int,SubString(@cL_LSH,7,2))=DatePart(Day,Getdate())
  Begin
  Select @nL_CDM=Len(Rtrim(Convert(Char(20),Convert(Int,Right(@cL_LSH,12))+1)))
  Update LSHB Set LSH=Left(@cL_LSH,8)+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(20),Convert(Int,Right(@cL_LSH,12))+1))
  Where MC=@cL_MC 
  End
  Else
  If @nL_Init>1
  Update LSHB set LSH=replace(Convert(Char(10),getdate(),102),'.','')+Replicate('0',@nL_CDT-@nL_CDM)+RTrim(Convert(Char(10),@nL_Init))
  where MC=@cL_MC
  Else
  Update LSHB set LSH=replace(Convert(Char(10),getdate(),102),'.','')+Replicate('0',@nL_CDT-1)+'1'
  where MC=@cL_MC
  End
  If  @cL_LX='4'
  begin  -- 前面四位为年 '00' ,年用两位表示
  Select @nL_CDT=@nL_CD-2
  If  left(@cL_LSH,2)=convert(char(2),getdate(),2)  /*  年份相等,序号加 1  */
  begin
  Select @nL_CDM=Len(RTrim(Convert(Char(20),Convert(int,Right(@cL_LSH,18))+1)))
  Update LSHB set LSH=Left(@cL_LSH,2)+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(20),Convert(int,Right(@cL_LSH,18))+1))
  where MC=@cL_MC
  End
  else
  If @nL_Init>1
  Update LSHB set LSH=replace(Convert(Char(2),getdate(),2),'.','')+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(10),@nL_Init))
  where MC=@cL_MC
  Else
  Update LSHB set LSH=replace(Convert(Char(2),getdate(),2),'.','')+Replicate('0',@nL_CDT-1)+'1'
  where MC=@cL_MC
  End
  If  @cL_LX='5'
  begin  -- 前面六位为年月 '0008', 年用两位
  Select @nL_CDT=@nL_CD-2
  If  left(@cL_LSH,2)=convert(char(2),getdate(),2) AND Convert(int,substring(@cL_LSH,3,2))=DATEPART(month,getdate())
  /*  年月相等,序号加 1  */
  begin
  Select @nL_CDM=Len(RTrim(Convert(Char(20),Convert(int,Right(@cL_LSH,16))+1)))
  Update LSHB set LSH=Left(@cL_LSH,4)+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(20),Convert(int,Right(@cL_LSH,16))+1))
  where MC=@cL_MC
  End
  else
  If @nL_Init>1
  Update LSHB set LSH=replace(Convert(Char(5),getdate(),2),'.','')+Replicate('0',@nL_CDT-@nL_CDM)+RTrim(Convert(Char(10),@nL_Init))
  where MC=@cL_MC
  Else
  Update LSHB set LSH=replace(Convert(Char(5),getdate(),2),'.','')+Replicate('0',@nL_CDT-1)+'1'
  where MC=@cL_MC
  End
  If  @cL_LX='6'
  begin  -- 前面六位为年月日 '000910'
  Select @nL_CDT=@nL_CD-6
  If  left(@cL_LSH,2)=convert(char(2),getdate(),2) AND Convert(int,substring(@cL_LSH,3,2))=DATEPART(month,getdate())
  AND Convert(Int,SubString(@cL_LSH,5,2))=DatePart(Day,Getdate())
  Begin
  Select @nL_CDM=Len(Rtrim(Convert(Char(20),Convert(Int,Right(@cL_LSH,14))+1)))
  Update LSHB Set LSH=Left(@cL_LSH,6)+Replicate('0',@nL_CDT-@nL_CDM)+Rtrim(Convert(Char(20),Convert(Int,Right(@cL_LSH,14))+1))
  Where MC=@cL_MC 
  End
  Else
  If @nL_Init>1
  Update LSHB set LSH=replace(Convert(Char(8),getdate(),2),'.','')+Replicate('0',@nL_CDT-@nL_CDM)+RTrim(Convert(Char(10),@nL_Init))
  where MC=@cL_MC
  Else
  Update LSHB set LSH=replace(Convert(Char(8),getdate(),2),'.','')+Replicate('0',@nL_CDT-1)+'1'
  where MC=@cL_MC
  End
  end
  CLOSE LSHB_cursor
  DEALLOCATE LSHB_cursor
  select LSH from LSHB where MC=@cL_MC
End 

GO


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-991826/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-991826/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值