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 2000 Forums
 SQL Server Development (2000)
 Getting a column value from another table which contains its field name

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-01-12 : 08:32:25
Jenna W. Jackson writes "I have two tables - Table A and Table B

Table A has field "tblBFieldName" with the value of "Gender", "ColID" with the value of "1"

"Gender" is a field in Table B.

My SQL Statement is: "Select tblBFieldName from TableA where ColID=1"

I would like to get the value of Gender in Table B based on the results in the above-mentioned SQL statement using just 1 call to the SQL server.

Any tips or advice is very much appreciated."

Kristen
Test

22859 Posts

Posted - 2005-01-12 : 08:52:32
You'll need to use some dynamic SQL:

DECLARE @strSQL varchar(8000)

SELECT @strSQL = 'SELECT '
+ (Select MAX(tblBFieldName) from TableA where ColID=1)
+ ' FROM TableB'
-- PRINT @strSQL -- Debug Use Only!!
EXEC (@strSQL)

Kristen
Go to Top of Page
   

- Advertisement -