I am struck at writing a query.i searched and didn't find any solution to this. Here i want to show the column name based on some specific value
For Instance , My table is like.
id | fruits |vegetables |softdrink
-----------------------
1 | apple | Onion | Pepsi
2 | mango | Potato | Coke
3 | banana | Bringal | RedBull
if i have a value "mango", then i should get the column name as fruit or
if i have a value "RedBull", then i should get the column name as softdrink
Below query is used in MySQL :
set @q= CONCAT('SELECT columns.column_name
from table inner
join information_schema.columns
on columns.table_schema = "dbname"
and columns.table_name = "table"
and ((',
(SELECT GROUP_CONCAT(CONCAT('columns.column_name="',column_name,'"',' and table.',column_name,' = "value','"') SEPARATOR ' OR ')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table'),
'))');
prepare query from @q;
execute query;
I want write same query in sql server 2008 for Get column name dynamically by Specific row value.Solution appreciated.how to replace mysql query to sqlserver 2008