Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Getting actual field names from another table.

Author  Topic 

liangtp@hotmail.com
Starting Member

27 Posts

Posted - 2008-08-06 : 03:47:10
Hi,

I wonder if my problem can be solved by SQL statements, without using client code. Thanks in advance.

In table tblA, I have below.
----------------------------
Field1, Field2, Field3, Field4
The Hulk,1970-07-31 11:25:13,M,2008-07-31 11:25:13
The Champ,1970-07-31 11:25:13,M,2008-07-31 11:25:13
Rocky,1970-07-31 11:25:13,M,2008-07-31 11:25:13
The Rock,1970-07-31 11:25:13,M,2008-07-31 11:25:13

In table tblB, I have the below:
--------------------------------
RecNo, FieldName,
1 Name
2 MemberSince
3 Sex
4 DOB

The actual field names of tblA can be found in tblB, where Field1 in tblA maps to Recno 1 of tblB, Field2 maps to Recno 2, and so on.

I wish to have the final result to be as such:
Name, MemberSince, Sex, DOB
The Hulk,1970-07-31 11:25:13,M,2008-07-31 11:25:13
The Champ,1970-07-31 11:25:13,M,2008-07-31 11:25:13
Rocky,1970-07-31 11:25:13,M,2008-07-31 11:25:13
The Rock,1970-07-31 11:25:13,M,2008-07-31 11:25:13

Can you help?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-06 : 03:49:11
Bad design ! You have to use Dynamic SQL to do what you want. see http://www.sommarskog.se/dynamic_sql.html


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -