您有此请求:
SELECT COL1, COL2, COL3 FROM TEST
我还有另一张包含这些数据的表格:
-----------------
| COL | VALUE |
-----------------
| COL1 | TOTO |
-----------------
| COL2 | TATA |
-----------------
| COL3 | TITI |
-----------------
我希望请求的结果集看起来像:
---------------------
| TOTO| TATA | TITI |
---------------------
而不是
---------------------
| COL1| COL2 | COL3 |
---------------------
感谢您的帮助
答案 0 :(得分:0)
我不知道它是否可行,但如果是,你将需要做一个子查询。
类似的东西:
SELECT
COL1 as (SELECT value FROM anothertablewiththesedata WHERE condition),
COL2 as (SELECT value FROM anothertablewiththesedata WHERE condition),
COL3 AS (SELECT value FROM anothertablewiththesedata WHERE condition)
FROM TEST
也许这很棘手或不可能。
有人可以确认或纠正我吗? :)
答案 1 :(得分:0)
您可以使用动态SQL来实现它。
declare @sql varchar(max)
set @sql='SELECT
COL1 as ' + (SELECT value FROM anothertablewiththesedata WHERE col = 'col1') + ' ,
COL2 as ' + (SELECT value FROM anothertablewiththesedata WHERE col = 'col2') + ' ,
COL3 as ' + (SELECT value FROM anothertablewiththesedata WHERE col = 'col3') + ' ,
FROM TEST'
exec @sql