select - How to get column name of particular value in sql server 2008 -
i want column name of particular value. have table currentreport having unique id can particular row values of row want name of columns need update .
i think want list of column names match particular value.
to can create xml column in cross apply each row , use nodes() in second cross apply shred on elements has value looking for.
ms sql server 2014 schema setup:
create table dbo.currentreport ( id int primary key, col1 varchar(10), col2 varchar(10), col3 varchar(10) ); go insert dbo.currentreport(id, col1, col2, col3) values(1, 'value1', 'value2', 'value3'); insert dbo.currentreport(id, col1, col2, col3) values(2, 'value2', 'value2', 'value2'); insert dbo.currentreport(id, col1, col2, col3) values(3, 'value3', 'value3', 'value3');
query 1:
-- value declare @value varchar(10) = 'value2'; select c.id, -- element name xml v.x.value('local-name(.)', 'sysname') columnname dbo.currentreport c cross apply ( -- build xml each row select c.* xml path(''), type ) x(x) -- nodes value = @value cross apply x.x.nodes('*[text() = sql:variable("@value")]') v(x);
| id | columnname | |----|------------| | 1 | col2 | | 2 | col1 | | 2 | col2 | | 2 | col3 |
Comments
Post a Comment