我在這裏搜索了一些主題,但沒有任何我需要的答案。 我想做一個查詢,我將根據第一個表中的列名加入一個表。從列表中選擇記錄作爲列名。動態sql
我正在使用sql server,所以如果有人知道這種技術的解決方案,將不勝感激。
這裏有一個例子我想做的事:
表:
main_table
----------
id | tab | another_col
----------------------
1 | product_x | abcd
2 | product_y | efgh
table_product_x
----------------------
id | yyy
----------------------
1 | simple_yyy_value1
table_product_y
----------------------
id | yyy
----------------------
2 | simple_yyy_value4
輸出:
product_x | simple_yyy_value1 | abcd
product_y | simple_yyy_value4 | efgh
查詢(草圖)
select tab, yyy, another_col from main_table
join 'table_'+tab xxx on xxx.id = main_table.id
這將是有益的,如果你可以添加一些樣本數據,你的表和預期產出的問題。 – 2012-03-02 22:39:09
已經更新 – nosbor 2012-03-02 22:49:10