I have a table dbo.EX_DD12
that shows data, but the table columns have a code assigned ( DD12001, DD12002, DD12003....DD12055
).
This query:
SELECT * FROM [dbo.EX_DD12]
returns results something like this:
Id | DD12001 | DD12002 | DD12003
---+------------+------------+--------
1 | 12/12/2005 | 10:00 a.m. | x
2 | 13/12/2005 | 10:00 a.m. | y
3 | 14/12/2005 | 09:00 a.m. | x
Using this query I can get the description:
SELECT
T0.name, T1.Description
INTO
#TABLE00
FROM
sys.columns T0
INNER JOIN
dbo.EXDT T1 ON T0.name = Code
WHERE
object_id = OBJECT_ID('dbo.EX_DD12')
AND T0.name LIKE 'DD12%'
The results look something like this:
Code | Description
--------+-------------
DD12001 | Date
DD12002 | Time
DD12003 | Priority
What can I do so that in a query the results looks like this?
Id | Date | Time | Priority
---+------------+------------+---------
1 | 12/12/2005 | 10:00 a.m. | x
2 | 13/12/2005 | 10:00 a.m. | y
3 | 14/12/2005 | 09:00 a.m. | x
1条答案
按热度按时间polhcujo1#
In SQL Server:
Building a query dynamically and executing with
sp_executesql
:rextester demo: http://rextester.com/GGEL20070
returns:
dynamic sql reference:
sp_executesql