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.
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?