Forum guidelines
  • Home
  • »
  • .NET General
  • »
  • how to switch rows of table of sql to column of gridview
AuthorQuestion
how to switch rows of table of sql to column of gridview
Posted on: 21 Aug 2013




how to switch rows of a table of sql server to column of gridview ?


AuthorReply
Re: how to switch rows of table of sql to column of gridview
Posted on: 21 Aug 2013  
Hi,
        Create a procedure in your sql for the below query and then call your stored procedure from your code

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(ColumnName) 
                    from yourtable
                    group by ColumnName, id
                    order by id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT ' + @cols + ' from 
             (
                select value, ColumnName
                from yourtable
            ) x
            pivot 
            (
                max(value)
                for ColumnName in (' + @cols + ')
            ) p '

execute(@query)

Regards,
Iftikar

Remember to click "Mark as Answer" on the post, if it helps you.

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.