sql 流水号

CREATE TABLE [dbo].[SriaNum] (
[Num] [int] NOT NULL
)

alter PROC dpIDS_GetSerialNumber
@SerialNumber VARCHAR(4) OUTPUT -- 指明为输出参数
AS
IF NOT EXISTS(SELECT
*
FROM
SriaNum)
BEGIN
INSERT INTO SriaNum values(1)
END
ELSE
BEGIN
UPDATE SriaNum SET Num=Num+1
END

SELECT
@SerialNumber = REPLICATE('0',4-LEN(Num))+CONVERT(VARCHAR(4),Num) --生成[000000001, 999999999]范围内的流水号
FROM
SriaNum

---------------------
DECLARE @TEST VARCHAR(4)
EXECUTE [dbo].dpIDS_GetSerialNumber @TEST OUTPUT -- 指明为输出变量
SELECT @TEST AS SERIALNUMBER -- 获得流水号

你可能感兴趣的:(sql)