SGGL_JT/DataBase/SpGetNewCode5-2022-10-23-00...

35 lines
1.8 KiB
Transact-SQL
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.


/****** Object: StoredProcedure [dbo].[SpGetNewCode5] Script Date: 2022/10/23 11:41:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SpGetNewCode5]
@tableName varchar(50),/*表名*/
@columnName varchar(30),/*列名*/
@prefix varchar(50),/*流水号编码前缀*/
@returnVal nvarchar(50) output
AS
/*获取危险源代码的流水号*/
declare
@sql nvarchar(500),
@old nvarchar(50),
@newid varchar(50),
@maxId nvarchar(50)/*已分配的最大值*/
select @sql=N'SELECT @maxId=MAX(right('+@columnName+',9)) from '+@tableName+' where ' + @columnName+' like ' + '''%' + @prefix + '%'''
EXEC sp_executesql @sql, N'@maxId nvarchar(50) OUTPUT', @maxId OUTPUT
set @old=@maxId
IF(@old is null)
BEGIN
set @newid=@prefix+'00001'
END
ELSE
BEGIN
set @newid = (select max(right(@old,5)))+1
set @newid = @prefix +right('00000'+@newid,5)
END
SET @returnVal=@newid