Question

In the following query, the formid parameter is causing an error. I have tried using a static value in place of the formid parameter, in which case the query succeeds. Am I using improper syntax? This thread appear to solve the issue, but the syntax seems to be the same.

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)
Was it helpful?

Solution

convert it to string,

CAST(@formid AS VARCHAR(25))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top