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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using column name in select statement?? 1

Status
Not open for further replies.

bdotzour

Programmer
Jun 7, 2001
17
US
I want to do this:

create procedure simple
@col char(40)
as
select * from @col

But that doesn't work. Can anyone help me find out how to pass a variable containing a column name and then use that variable in the select statement?

Thanks!
 
Column names belong in the Select List. Table names belong in the From clause.

You can do the following.

create procedure simple
@col varchar(40)
as
declare @sql nvarchar(256)
Set @sql="select " + @col + " From tblname"
exec sp_executesql @sql
Go
Terry
------------------------------------
Experience is the hardest kind of teacher. It gives you the test first, and the lesson afterward.
 
Fantastic! Of course you were right about the mixup, but i'm glad you knew what i meant.

Thanks so much. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top