我要求传递给SP的输入参数的类型为numeric(18,6)。并将此参数传递给动态sql查询(类型varchar),结果将存储到临时表中。在临时表中,我将上述列类型定义为仅数字形式。但是在执行SP时,出现“将数据类型varchar转换为数字时出错”。 使用动态Sql是否有限制?有人请帮助我。
示例查询:
-------------
Input Parameter: @DataItem numeric(18,6) = ABC054356 (column name)
CREATE TABLE #Temp1(RowNum int,
CompanyID int,
ABCColumn numeric(18,2),
FiscalYear varchar(100))
DECLARE @QUERY varchar(max)
SET @QUERY = N'select row_number() over (partition by fy.keyinstn order by fy.fiscalyear desc) as RowNum, fy.Keyinstn, '
+ Convert(@DataItem as numeric(18,2)))+
', period.fiscalyear from ABCTable
................
................
INSERT into #Mnemonic
EXEC(@query)
答案 0 :(得分:0)
您应该在连接前将数值转换为字符串
SET @QUERY = N'select row_number() over (partition by fy.keyinstn order by fy.fiscalyear desc) as RowNum, fy.Keyinstn, '
+ CONVERT(VARCHAR(20), @DataItem)+
', period.fiscalyear from ABCTable ....... '
答案 1 :(得分:0)
您显然在表中传入了一个列名称,然后您希望将其转换。
如果是这样,那么看起来就像
declare @sql nvarchar(max);
set @sql = N'
select row_number() over (partition by fy.keyinstn order by fy.fiscalyear desc) as RowNum,
fy.Keyinstn,
convert(decimal(18, 2), [DataItem]) as [DataItem],
period.fiscalyear
from ABCTable
. . .
';
set @sql = replace(@sql, '[DataItem]', @DataItem);
insert into #Mnemonic
exec(@query);