The following SQL outputs a dynamic no of columns. The data is in the exact initial format for preparing a string to pass to a google chart. Problem is, I get nice output to management studio results (as a table). But now I want to loop these results and build a string to return as chart source..
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
SELECT @cols = STUFF((SELECT distinct 
                        ',' +
                        QUOTENAME(MeterReadType)
                 FROM MeterReadView with (nolock)
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'');
SET @query = ' SELECT MeterReadMonth as Month,  ' + @cols + ' 
FROM 
(
   SELECT MeterReadType, MeterReadMonth,isnull(total,0) as total
   FROM [MeterReadView] with (nolock)
) t
PIVOT
( 
  sum(total)
  FOR MeterReadType IN (' + @cols + ' )
) 
p ' ;
Execute(@query);
The above SQL outputs like...
Month   Desc 1, Desc 2, Desc 3, etc and so on.. 
2014-06 4       5       66
2014-06 9       3       7
But I need to loop results and build a string in the following format. I cannot just load into MVC because I need to specify a class and I don't know how many columns there will be, so i just build a string in SQL. The latter of which I can do. Just not sure how to extend this existing SQL to go into a loop because its execute(@query), how to get that into a temp table that knows the no of columns?
     [['Month','Desc 1','Desc 2','Desc 3'],
     ['Jun-14',4,5,66],
     ['Jun-14',9,3,7], 
['May-15',20,66,5]]
 
     
     
    