列名NA无效

时间:2013-08-26 20:11:25

标签: sql-server-2008 tsql dynamic-sql

我正在尝试使用以下插入语句,但我没有运气。我已经研究过很多东西,但还没有找到解决方案。无论如何都要将文字字符串NA分配给列?

qryALL (@pPK BIGINT,@pAL FLOAT,@pFK BIGINT)
AS
DECLARE @query  AS NVARCHAR(MAX)

SET @query = 'INSERT INTO tblTPF ( fldFPK, fldPK, fldDTA, fldCI, fldMD, fldME, 
fldIQ, fldSD, fldND, fldNI, fldNL, fldMI, fldMA, fldUN, fldAV )
SELECT '+ Convert(varchar,@pFK) +' AS sFPK, '+ Convert(varchar,@pPK) +' AS sPK, 
GETDATE() AS sDate, '+ Convert(varchar,@pAL) +' AS sAL, -254 AS sMD, -254 AS sME,
-254 AS sIQ, -254 AS sSD, 0 AS sND, 0 AS sNI, 0 AS sNL, -254 AS sMI, -254 AS sMA, 
NA AS sUN, 0 AS sAV;'

execute sp_executesql @query

1 个答案:

答案 0 :(得分:1)

字面字符串应该用单引号括起来,例如: select 'NA' as sUN,字符串中的文字字符串应该用双引号括起来:

SET @query = 'INSERT INTO tblTPF ( fldFPK, fldPK, fldDTA, fldCI, fldMD, fldME, 
fldIQ, fldSD, fldND, fldNI, fldNL, fldMI, fldMA, fldUN, fldAV )
SELECT '+ Convert(varchar,@pFK) +' AS sFPK, '+ Convert(varchar,@pPK) +' AS sPK, 
GETDATE() AS sDate, '+ Convert(varchar,@pAL) +' AS sAL, -254 AS sMD, -254 AS sME,
-254 AS sIQ, -254 AS sSD, 0 AS sND, 0 AS sNI, 0 AS sNL, -254 AS sMI, -254 AS sMA, 
''NA'' AS sUN, 0 AS sAV;'