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)
 How to use local variables as fieldnames.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-01 : 09:04:29
Hugo writes "Hi,

I would like to declare a local variable in T-SQL and use its value as a fieldname.

I tried the following code:

use pubs
DECLARE @lname char(30)
SET @lname = 'au_fname'

SELECT @lname FROM authors

I expected to see al list of firs names from authors. Why doesn't this work and is there a way of achieving this?

regards,

Hugo."

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2002-03-01 : 09:04:29
You need to use dynamic SQL: http://www.sqlteam.com/FilterTopics.asp?TopicID=157
Go to Top of Page
   

- Advertisement -