DB生成唯一Code
思路:创建标量,得到Code字段最大值转为int再加1
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--不同表 1:Products 2:InsideList 3:Sale 4:SaleBack 5:Payment 6:Gathering 7:CheckStock
ALTER FUNCTION [dbo].[GetCode]
(
@TableNameFlag varchar(2)
)
RETURNS varchar(max)
AS
BEGIN
DECLARE @Code varchar(max)
DECLARE @num int
set @Code='00000001'
if @TableNameFlag='1'
set @num=(select max(Convert(int,ProCode))+1 as Code from Products)
if @TableNameFlag='2'
set @num=(select max(Convert(int,InsideCode))+1 as Code from InsideList )
if @TableNameFlag='3'
set @num=(select max(Convert(int,SaleNO))+1 as Code from Sale )
if @TableNameFlag='4'
set @num=(select max(Convert(int,SaleBackNO))+1 as Code from SaleBack )
if @TableNameFlag='5'
set @num=(select max(Convert(int,Code))+1 as Code from Payment )
if @TableNameFlag='6'
set @num=(select max(Convert(int,Code))+1 as Code from Gathering )
if @TableNameFlag='7'
set @num=(select max(Convert(int,Code))+1 as Code from CheckStock )
if @num<10
set @Code='0000000'+CAST(@num as varchar(max))
else if @num<100
set @Code='000000'+CAST(@num as varchar(max))
else if @num<1000
set @Code='00000'+CAST(@num as varchar(max))
else if @num<10000
set @Code='0000'+CAST(@num as varchar(max))
else if @num<100000
set @Code='000'+CAST(@num as varchar(max))
else if @num<1000000
set @Code='00'+CAST(@num as varchar(max))
else if @num<10000000
set @Code='0'+CAST(@num as varchar(max))
else if @num<100000000
set @Code=CAST(@num as varchar(max))
RETURN @Code
END