sql server - Loop through columns having name T-SQL -


i have query:

  select ac.*,cp.nomecampo fieldname optes op      inner join  artico art on art.id=op.idartico     inner join sam.artcla3id13 ac on ac.idartico=art.id      inner join campipers cp on cp.tabella = 'artcla3id3' op.id = 54782.000000  

that returns this:

    rivestimento | numtaglienti | raggio | diamscarico | fieldname    |     ______________________________________     nuda         |       0      |    0   |     1     |  diamscarico |     nuda         |       0      |    0   |     1     |  diamscarico | 

how can have this?

diamscarico |  1 raggio      |  0 numtaglienti|  0 rivestimento|  nuda 

thanks!

you can put result of query xml variable , unpivot when querying xml.

declare @xml xml  set @xml =    (     -- query goes here     select *     yourtable     -- here     xml path(''), type   )  select t.x.value('local-name(.)', 'sysname') columnnaame,        t.x.value('./text()[1]', 'nvarchar(max)') value @xml.nodes('*') t(x) 

sql fiddle


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -