在以下查询中,formid 参数导致错误。我尝试使用静态值代替 formid 参数,在这种情况下查询成功。我是否使用了不正确的语法?这个线程似乎解决了这个问题,但语法似乎是一样的。
ALTER PROCEDURE [dbo].[customFormReport]
(
@formid int
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(fieldname)
from FormResponse
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT FormID, FormSubmissionID,' + @cols + ' from
(
SELECT FormID, FormSubmissionID, fieldname, value
FROM FormResponse WHERE FormID = ' + @formid + '
) x
pivot
(
max(value)
for fieldname in (' + @cols + ')
) p '
execute(@query)