表tblidset在第一次运行时不包含任何内容,然后当我第一次在我的代码中调用此存储过程时,它应该从表中获取最高值,以便稍后在程序数据上构建该值增长。让我烦恼的问题是它在第一次运行时总是返回0。坚持了几个小时,现在傻眼了。关心帮忙?这是我的剧本:
CREATE PROCEDURE CREATENEWID
@TYPE AS INT
AS
BEGIN
DECLARE @CNT AS INT;
DECLARE @DOCNO AS BIGINT;
DECLARE @DOCID AS BIGINT;
SELECT @CNT = ISNULL(COUNT(*),0) FROM TBLIDSET
--CHECK IF THERE IS NO VALUE AS OF DATE AND INSERT DEFAULT VALUES
IF @CNT = 0
BEGIN
INSERT INTO TBLIDSET SELECT TOP 1(DOCID),0 FROM INVHDR ORDER BY DOCID DESC
END
DECLARE @NUM AS INT;
IF @TYPE = 1
BEGIN
UPDATE TBLIDSET SET DOCID = DOCID +1
SELECT @NUM = DOCID FROM TBLIDSET
END
ELSE
BEGIN
UPDATE TBLIDSET SET DOCNO = DOCNO + 1
SELECT @NUM = DOCNO FROM TBLIDSET
END
PRINT @NUM
RETURN @NUM
END
GO
答案 0 :(得分:0)
请尝试此代码。我将输出参数添加到您的代码中。
CREATE PROCEDURE CREATENEWID
@TYPE AS INT,
@NUM AS INT OUT --Declare ouput parameter here
AS
BEGIN
DECLARE @CNT AS INT;
DECLARE @DOCNO AS BIGINT;
DECLARE @DOCID AS BIGINT;
SELECT @CNT = ISNULL(COUNT(*),0) FROM TBLIDSET
--CHECK IF THERE IS NO VALUE AS OF DATE AND INSERT DEFAULT VALUES
IF @CNT = 0
BEGIN
INSERT INTO TBLIDSET SELECT TOP 1(DOCID),0 FROM INVHDR ORDER BY DOCID DESC
END
--Move your @NUM to output parameter
IF @TYPE = 1
BEGIN
UPDATE TBLIDSET SET DOCID = DOCID +1
SELECT @NUM = DOCID FROM TBLIDSET
END
ELSE
BEGIN
UPDATE TBLIDSET SET DOCNO = DOCNO + 1
SELECT @NUM = DOCNO FROM TBLIDSET
END
PRINT @NUM
RETURN @NUM
END
GO