当前位置: 动力学知识库 > 问答 > 编程问答 >

sql - Select columns from a table according to result list from another table

问题描述:

I have two tables as follows.

--TABLE1 - CONTAINS SOME COLUMN NAMES OF TABLE2

ID | COL_NAME

1 | FIRST_NAME

2 | LAST_NAME

3 | GENDER

--TABLE2 - CONTAINS THE ACTUAL VALUES

ID | FIRST_NAME | LAST_NAME | AGE | GENDER

10 | A | Z| 30 | M

12 | B | Y | 32 | F

15 | C | X | 21 | M

I want to write a query in SQL SERVER 2005 to fetch records from TABLE2 with only those columns which are exists in TABLE1.

E.g.

I want to select only FIRST_NAME, LAST_NAME AND GENDER columns from TABLE2 and should not include AGE as AGE is not there in TABLE1.

How can I achieve this with or without using a dynamic query?

分享给朋友:
您可能感兴趣的文章:
随机阅读: