我在这个网站上很新。我有一个查询,枢轴2加入查询。我在'附近使用'获得了错误的语法。如果此语句是公用表表达式,xmlnamespaces
子句或更改跟踪上下文子句,则必须以分号结束前一个语句。我一直试图用分号,但我还是不能让它发挥作用。
您可以在此处查询。
Declare @sql nvarchar(max)
Declare @columns nvarchar(max)
declare @orgcols table(
[QuestionID] nvarchar(max)
)
insert into @orgcols
Select distinct a.[QuestionID] + ' - ' + a.[GURSRVQ_TEXT] as [QuestionID]
from [MappingNew4] a
left join [ICE_SURVEY_ANSWERS_MVCleaned] b
on a.[GUBSRVY_TITLE] = b.[GUBSRVY_TITLE]
join [ICE_SURVEY_ANSWERS_SUMMARY_MV] c
on b.[GUBSRVY_TITLE] = c.[GUBSRVY_TITLE]
where TERM = 'ABC';
;With combinedcolumntab as(
select distinct
STUFF((Select ','+[QuestionID]
from @orgcols T1
FOR XML PATH('')),1,1,'') as col from @orgcols T2
)
select @columns = col from combinedcolumntab
set @sql = N'With org as
(Select
b.TOKEN,a.[GUBSRVY_TITLE], a.[QuestionID] + '' - '' + a.[GURSRVQ_TEXT] as [QuestionID] ,replace(replace((cast(b.[GURSRVQ_TEXT] as NVarchar(100))),'','','';''),'''''''','''''''''''') as[GURSRVQ_TEXT],(cast(isnull([GORSRVR_RESPONSE_1], 0) as int)
+ cast(isnull([GORSRVR_RESPONSE_2],0) as int) + cast(isnull([GORSRVR_RESPONSE_3],0) as int)
+ cast(isnull([GORSRVR_RESPONSE_4],0) as int) + cast(isnull([GORSRVR_RESPONSE_5],0) as int))
as RESPONSE
from [MappingNew4] a
left join [ICE_SURVEY_ANSWERS_MVCleaned] b
on a.[GUBSRVY_TITLE] = b.[GUBSRVY_TITLE]
join [ICE_SURVEY_ANSWERS_SUMMARY_MV] c
on b.[GUBSRVY_TITLE] = c.[GUBSRVY_TITLE]
where TERM = ''ABC''
)
Select *
From Org
PIVOT
(
sum(RESPONSE)
FOR QuestionID
in ('+@columns+')
) as pvt'
EXECUTE sp_executesql @sql
答案 0 :(得分:0)
您用于插入select
的{{1}}语句,请尝试将其更改为:
@orgcols
这修复了我得到的唯一错误并使其工作(虽然我猜测了一些表数据类型等,因为你没有在你的问题中包含这些信息...)