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‘‘[email protected];
if(LEN(@day)=1) set @day=‘‘0‘‘[email protected];
-- WITH(XLOCK,PAGLOCK)
set @docNo = @preFix+cast(YEAR(getdate()) as varchar)+ @[email protected];
if @preFix=‘‘WV‘‘ or @preFix=‘‘SR‘‘ or @preFix=‘‘SA‘‘
set @sql =N‘‘select top 1 @p=code from ‘‘[email protected] +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘[email protected]+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
else
set @sql =N‘‘select top 1 @p=code from ‘‘[email protected] +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘[email protected]+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
EXEC sp_executesql @sql,N‘‘@p varchar(256) output‘‘,@[email protected] 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 @[email protected]+1;
set @no=‘‘0‘‘[email protected];
end
set @[email protected]+1
if @preFix=‘‘WV‘‘ or @preFix=‘‘SR‘‘ or @preFix=‘‘SA‘‘
insert into @tmptable select @preFix,@[email protected]
else
insert into @tmptable select @preFix,@[email protected]
end;
insert into documentno (Prefix,Code) select Prefix,Code from @tmptable ;
--select @[email protected];
--返回最大的流水号
if @preFix=‘‘WV‘‘
set @outCode=‘‘W‘‘[email protected];
else
set @[email protected][email protected];
--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‘‘[email protected];
if(LEN(@day)=1) set @day=‘‘0‘‘[email protected];
set @docNo = @preFix+cast(YEAR(getdate()) as varchar)+ @[email protected];
if @preFix=‘‘WV‘‘
set @sql =N‘‘select top 1 @p=code from ‘‘[email protected] +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘[email protected]+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
else
set @sql =N‘‘select top 1 @p=code from ‘‘[email protected] +‘‘ WITH(holdlock,tablockx) where CHARINDEX(‘‘‘‘‘‘[email protected]+‘‘‘‘‘‘,code)=1 order by id desc‘‘;
EXEC sp_executesql @sql,N‘‘@p varchar(256) output‘‘,@[email protected] 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 @[email protected]+1;
set @no=‘‘0‘‘[email protected];
end
if @preFix=‘‘WV‘‘
begin
insert into documentno(Prefix,Code) values(@preFix,@[email protected]);
set @outCode=‘‘W‘‘[email protected];
end
else
begin
insert into documentno(Prefix,Code) values(@preFix,@[email protected]);
--select @[email protected];
set @[email protected][email protected];
end
END
原文地址:https://www.cnblogs.com/lionking/p/11619892.html