Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->编程语言 ->Delphi ->正文

自动编号的存储过程

来源:Linuxdby.com 作者:Webmaster 时间:2007-06-05 点击: [收藏] [投稿]
 

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 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())

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:《delphi高手突破》节选一   下一篇:《delphi高手突破》节选二

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号