1

在以下查询中,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)
4

1 回答 1

0

将其转换为字符串,

CAST(@formid AS VARCHAR(25))
于 2013-05-29T13:13:02.393 回答