0

我正在尝试使以下插入语句起作用,但我没有运气。我已经研究并尝试了很多东西,但还没有找到解决方案。无论如何将文字字符串 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
4

1 回答 1

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;'
于 2013-08-26T20:13:51.437 回答