存储过程代码:
CREATE PROCEDURE [dbo].[GetNumber]
@rst AS varchar(10) OUTPUT
as
BEGIN
DECLARE @y VARCHAR(20);
DECLARE @m VARCHAR(2);
--取得年份和月份
set @y=DATEPART(yyyy,GETDATE());
set @y= RIGHT(convert(varchar,@y),2)
--月份拼凑
set @m=DATEPART(mm,GETDATE());
DECLARE @lg int;
SELECT @lg=len(@m);
if @lg=1
BEGIN
set @m='0'+@m;
END
--获取当前数据库最大的ID
DECLARE @mx INT;
SELECT @mx=MAX(sindex) FROM data_number where ny=@y and nm=@m;
if @mx is NULL --判断是否为空
BEGIN
set @mx=1;
END
else
BEGIN
set @mx=@mx+1;
END
set @rst=REPLICATE('0',5-len(@mx))+CONVERT(VARCHAR,@mx);
set @rst=@y+@m+@rst;
INSERT into data_Number(ny,nm,number,sindex) values (@y,@m,@rst,@mx);
RETURN @rst;
END