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.

sql fiddle

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); 

results:

| id | columnname | |----|------------| |  1 |       col2 | |  2 |       col1 | |  2 |       col2 | |  2 |       col3 | 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -