Ramco Ramco

Ramco Ramco

  • 443
  • 3.4k
  • 527.1k

Error - is not a valid identifier.

Apr 20 2022 1:11 PM

Hi

 

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
Drop table #temp

Msg 203, Level 16, State 2, Line 23
The name 'select Name, [CURD 380 GM],[P. LASSI 1 LTR] from
(select name,quantity,itemname from #temp) x
pivot ( Sum(quantity) for itemname in ([Item1],[Item2])) piv ' is not a valid identifier.

 

Thanks


Answers (2)