Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variables

Status
Not open for further replies.

EPNICO

MIS
Jun 14, 2001
45
US
Hello:

Small question. Is it possible to use a local variable
that contains the name of the field in the select statement?

Example:

Declare @name char (17)

Set @name = 'c.coverage_code_1'

select @name
from #temptable c

Any ideas are welcomed.
Epnico




 
This is one of those questions whose answer will depend on the RDBMS/utility being used. For instance, you CAN do this with Oracle if you are using SQL*Plus (substitution variables) or PL/SQL (dynamic SQL). I'm sure you can do it with other RDBMSs; the details will vary.
 

In SQL Server, you must create and execute a dynamic SQL statement.

Declare @name char (17), @sql nvarchar(200)

Set @name = 'c.coverage_code_1'

Set @sql = 'select @name from #temptable c'
exec (@sql) Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top