Error - not a valid identifier

  • Hi

    ​   I am getting below error.

    Msg 203, Level 16, State 2, Line 20

    The name 'select Name, [Item1],[Item2]

    from (select name,quantity,itemname from #temp) x pivot (Sum(quantity) for itemname in ([Item1],[Item2])) piv ' is not a valid identifier.

    declare @cols as nvarchar(max)='';
    declare @query as nvarchar(max)='';
    select @cols = @cols + QUOTENAME(ItemName) + ',' from (Select distinct ItemName from #temp) as tmp
    select @cols = substring(@cols,0,len(@cols)) set @query = 'select Name, ' + @cols + '
    from (select name,quantity,itemname from #temp) x pivot (Sum(quantity) for itemname in (' + @cols + ')) piv ';
    execute @query

    Thanks

  • do execute (@query)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply