系统单据号生成存储过程
Posted lionking
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统单据号生成存储过程相关的知识,希望对你有一定的参考价值。
CREATE PROCEDURE [dbo].[GeneralDocumentNoByBatch]
@preFix nvarchar(256),@GenNum int ,@outCode varchar(256) output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
declare @docNo nvarchar(256),@month varchar(10),@day varchar(10),
@no varchar(10),@num int ,@lenNum int,@sql nvarchar(4000)
,@maxCode varchar(256),@tableName varchar(256)
SET NOCOUNT ON;
set @tableName=‘‘DocumentNo‘‘;
set @month=CAST(month(getdate())as varchar);
set @day = CAST(day(getdate())as varchar);
if(LEN(@month)=1) set @month=‘‘0‘‘+@month;
if(LEN(@day)=1) set @day=‘‘0‘‘+@day;
-- WITH(XLOCK,PAGLOCK)
set @docNo = @preFix+cast(YEAR(getdate()) as varchar)+ @month+@day;
if @preFix=‘‘WV‘‘ or @preFix=‘‘SR‘‘ or @preFix=‘‘SA‘‘
set @sql =N‘‘select top 1 @p=code from ‘‘+@tableName +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘+@preFix+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
else
set @sql =N‘‘select top 1 @p=code from ‘‘+@tableName +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘+@docNo+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
EXEC sp_executesql @sql,N‘‘@p varchar(256) output‘‘,@p=@maxCode output ;
if(@maxCode is null)
begin
set @no=‘‘0001‘‘;
end;
else
begin
if @preFix=‘‘WV‘‘ or @preFix=‘‘SR‘‘ or @preFix=‘‘SA‘‘
set @no=CAST((CAST(SUBSTRING(@maxCode,len(@preFix)+1,LEN(@maxCode)) as int)) as varchar);
else
set @no=CAST((CAST(SUBSTRING(@maxCode,len(@docNo)+1,LEN(@maxCode)) as int)) as varchar);
end;
--print @no
declare @tmptable table(preFix varchar(20),Code varchar(50))
--设定流水号位数
set @num =4;
declare @i int
set @i=0
while @i<@GenNum
begin
set @no=cast(cast(@no as int)+1 as varchar(10))
set @lennum = LEN(@no);
--print @lennum
while @lenNum<@num
begin
set @lenNum=@lenNum+1;
set @no=‘‘0‘‘+@no;
end
set @i=@i+1
if @preFix=‘‘WV‘‘ or @preFix=‘‘SR‘‘ or @preFix=‘‘SA‘‘
insert into @tmptable select @preFix,@preFix+@no
else
insert into @tmptable select @preFix,@docNo+@no
end;
insert into documentno (Prefix,Code) select Prefix,Code from @tmptable ;
--select @docNo+@no;
--返回最大的流水号
if @preFix=‘‘WV‘‘
set @outCode=‘‘W‘‘+@no;
else
set @outCode=@docNo+@no;
--select Prefix,Code from @tmptable
END
CREATE PROCEDURE [dbo].[GeneralDocumentNo]
@preFix nvarchar(256),@outCode varchar(256) output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
declare @docNo nvarchar(256),@month varchar(10),@day varchar(10),
@no varchar(10),@num int ,@lenNum int,@sql nvarchar(4000)
,@maxCode varchar(256),@tableName varchar(256)
SET NOCOUNT ON;
set @tableName=‘‘DocumentNo‘‘;
set @month=CAST(month(getdate())as varchar);
set @day = CAST(day(getdate())as varchar);
if(LEN(@month)=1) set @month=‘‘0‘‘+@month;
if(LEN(@day)=1) set @day=‘‘0‘‘+@day;
set @docNo = @preFix+cast(YEAR(getdate()) as varchar)+ @month+@day;
if @preFix=‘‘WV‘‘
set @sql =N‘‘select top 1 @p=code from ‘‘+@tableName +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘+@preFix+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
else
set @sql =N‘‘select top 1 @p=code from ‘‘+@tableName +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘+@docNo+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
EXEC sp_executesql @sql,N‘‘@p varchar(256) output‘‘,@p=@maxCode output ;
if(@maxCode is null)
begin
set @no=‘‘0001‘‘;
end;
else
begin
if @preFix=‘‘WV‘‘
set @no=CAST((CAST(SUBSTRING(@maxCode,len(@preFix)+1,LEN(@maxCode)) as int)+1) as varchar);
else
set @no=CAST((CAST(SUBSTRING(@maxCode,len(@docNo)+1,LEN(@maxCode)) as int)+1) as varchar);
end;
set @lennum = LEN(@no);
set @num =4;
while @lenNum<4
begin
set @lenNum=@lenNum+1;
set @no=‘‘0‘‘+@no;
end
if @preFix=‘‘WV‘‘
begin
insert into documentno(Prefix,Code) values(@preFix,@preFix+@no);
set @outCode=‘‘W‘‘+@no;
end
else
begin
insert into documentno(Prefix,Code) values(@preFix,@docNo+@no);
--select @docNo+@no;
set @outCode=@docNo+@no;
end
END
以上是关于系统单据号生成存储过程的主要内容,如果未能解决你的问题,请参考以下文章