michielbijnen (11/12/2012)
There is a comma! As far as I know the syntax is correct for the columns variable. Or not?
As an example
select 'Count IsPrimary', [0], [1]
from (select isprimary, blockid from block) b
pivot (
count(blockid)
for isprimary in ([0],[1])
) pWill work
Based on a quick glance at you procedure I'm guessing you producing something like
select 'Count IsPrimary',, [0], [1]
from (select isprimary, blockid from block) b
pivot (
count(blockid)
for isprimary in (,[0],[1])
) pWhich will generate a syntax error.
If you trim the leading comma off the columns you query should start working. But that is a guess as I can't test it. You may want to change exec(@query) to select @query and post the actual query that is being generated.