我有以下存储过程:
CREATE PROCEDURE [cafgAddCoreID]
@HoldingName nvarchar (50) = null
@CountStart int = 0,
AS
DECLARE @sql nvarchar (50)
DECLARE @bit nvarchar (200)
BEGIN
SET @bit = 'MTR1- + CAST ((' + @CountStart + ' + ROW_NUMBER()
OVER (ORDER BY [FindNo])) AS NVARCHAR(10))'
SET @sql = 'INSERT INTO ' + @HoldingName + '([CoreID])
SELECT (' + @bit + ') FROM [DectectoristMetalFinds]'
EXEC @sql
END
但是当我运行时:
EXEC [cafgAddCoreID] 'Table1', 9
我明白了
将 varchar 值 'MTR1- + CAST ((' )转换为数据类型 int 时转换失败。
跑步
INSERT INTO [Table1] ([CoreID])
SELECT ('MTR1-' + CAST ((0 + ROW_NUMBER() OVER (ORDER BY FindNo))
AS NVARCHAR(10))) AS CoreID FROM [Table2]
有效,所以我知道该方法是正确的,但显然不在存储过程中。