SQL流水帐号的生成

create proc aaa
(
        
@StockName varchar(50),
        
@StockCount int,
        
@StockPrice money
)
as
    
declare @StockID varchar(10);
    
declare @num int;
    
select @num = MAX(cast(substring(StockID,4,7as int)) from stock
    
if @num is null
    
begin
        
set @StockID = 'ST-' + '0000001'
        
insert into stock values(@StockID,@StockName,@StockCount,@StockPrice)
    
end
    
else
    
begin
        
set @num = @num + 1
        
set @StockID = 'ST-' + right('0000000' + replace(cast(@num as char(7)),' ',''),7)
        
insert into stock values(@StockID,@StockName,@StockCount,@StockPrice)
    
end
原文地址:https://www.cnblogs.com/cxy521/p/1100288.html