Hi, I have this dyanamic query in which I want to show the value of a variable @val1 as the column heading.
Declare @val1 varchar(20)
Select @val1='City'
Select id,
case
when table1.field1 = @val1 then table1.field2
end as 'xxxxxxxx' (this should be the value of @val1)
from table1
The result should look like:
ID City
123 Los Angeles
Next time when the value of @val1 is 'Department', then out put should look like
ID Department
123 IT
When I do case...end as @val1, its not working. The val1 can be a city, can be a departement etc.
Declare @val1 varchar(20)
Select @val1='City'
Select id,
case
when table1.field1 = @val1 then table1.field2
end as 'xxxxxxxx' (this should be the value of @val1)
from table1
The result should look like:
ID City
123 Los Angeles
Next time when the value of @val1 is 'Department', then out put should look like
ID Department
123 IT
When I do case...end as @val1, its not working. The val1 can be a city, can be a departement etc.