我試圖構建一個返回nvarchar值的過程。 它似乎工作得很好,直到我試圖從程序中獲得價值。 這是從我的過程中的所有代碼:SQL中的返回值爲空
ALTER PROCEDURE [dbo].[_MG_NextInventoryPackNumberForPartNumber]
@sPartNumber NVARCHAR(254) --##PARAM @sPartNumber The part number for which we need to get the new InventoryPackNo
AS
BEGIN
SET NOCOUNT ON;
DECLARE @num INT,
@sNewInventoryPackNum NVARCHAR(254),
@StringNum NVARCHAR(254)
SET @StringNum = (SELECT SUBSTRING(InventoryPackNo, 9, 5) AS pName
FROM InventoryPack
WHERE SUBSTRING(InventoryPackNo, 1, 8) LIKE @sPartNumber)
SET @num = CONVERT(INT, @StringNum)
SET @num = @num + 1
IF @num >= 1 AND @num <= 9
BEGIN
SET @StringNum = CONVERT(NVARCHAR(254), @num)
SET @StringNum = '0000' + @StringNum
END
IF @num >= 10 AND @num <= 99
BEGIN
SET @StringNum = CONVERT(NVARCHAR(254), @num)
SET @StringNum = '000' + @StringNum
END
IF @num >= 100 AND @num <= 999
BEGIN
SET @StringNum = CONVERT(NVARCHAR(254), @num)
SET @StringNum = '00' + @StringNum
END
IF @num >= 1000 AND @num <= 9999
BEGIN
SET @StringNum = CONVERT(NVARCHAR(254), @num)
SET @StringNum = '0' + @StringNum
END
IF @num >= 10000 AND @num <= 99999
BEGIN
SET @StringNum = CONVERT(NVARCHAR(254), @num)
END
IF @num = 100000
BEGIN
SET @StringNum = '00000'
END
SET @sNewInventoryPackNum = @sPartNumber + @StringNum + '10'
PRINT @sNewInventoryPackNum
RETURN @sNewInventoryPackNum
END
這是我嘗試調用的程序:
DECLARE @sNuwNum NVARCHAR(254)
EXEC @sNuwNum = _MG_NextInventoryPackNumberForPartNumber '77510002'
PRINT @sNuwNum
的程序應該返回這個值:775100020000210.但所有我在控制檯中看到屏幕部分是這樣的:
775100020000210
Msg 248, Level 16, State 1, Procedure _MG_NextInventoryPackNumberForPartNumber, Line 51
The conversion of the nvarchar value '775100020000210' overflowed an int column.
The '_MG_NextInventoryPackNumberForPartNumber' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
0
我使用的是SQL Server 2008的 我敢肯定,我要改變的東西小,但是你能不能認罪你告訴我什麼?
幾乎重複:[here](http://stackoverflow.com/questions/9837481/return-value-in-sql-server-stored-procedure)。 SQL Server始終返回int作爲返回值。爲此,請使用函數(UDF)。 – OzrenTkalcecKrznaric
重複的答案,也許。該問題的OP沒有明確的「RETURN」。 –