我正在尝试构建一个返回 nvarchar 值的过程。在我尝试从过程中获取价值之前,它似乎工作正常。这是我的程序中的所有代码:
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。我很确定我必须更改一些小东西,但是你能告诉我什么吗?