首页 > 代码库 > 创建自增系列号

创建自增系列号

DROP PROCEDURE CreateNextSerialNumber GO

CREATE PROCEDURE CreateNextSerialNumber  @SerialNumberPrefix VARCHAR(2),  @NextSerialNumber VARCHAR(10) OUT AS BEGIN  DECLARE @MaxSerialNumber VARCHAR(10),    @CurrentNumber VARCHAR(8),    @NextNumber VARCHAR(8),    @PrefixNumber VARCHAR(2)  BEGIN   SELECT @MaxSerialNumber = MAX(HIMPSerialNumber) FROM ClientMachineInfo   IF LEN(@SerialNumberPrefix) != 2    SET @PrefixNumber = ‘SN‘   ELSE    SET @PrefixNumber = @SerialNumberPrefix   IF ISNULL(@MaxSerialNumber, ‘‘) = ‘‘    BEGIN     SET @NextNumber = ‘00000000‘    END   ELSE    BEGIN     SET @CurrentNumber = RIGHT(@MaxSerialNumber, LEN(@PrefixNumber))     SET @NextNumber = RIGHT(‘00000000‘ + CONVERT(VARCHAR(8), CAST(@CurrentNumber AS INT) + 1), 8)    END   SET @NextSerialNumber = @PrefixNumber + @NextNumber   INSERT INTO ClientMachineInfo(HIMPSerialNumber, Used) VALUES(@NextSerialNumber, 1)  END END GO

创建自增系列号