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

How to get a column's value, which stored in variable @col 1

Status
Not open for further replies.

znet

Programmer
Aug 23, 2004
41
CA
Hello, All
I get the column names of a table, using system table/system stored procedure. Now the column name(for ex, 'ExtraField', but actually unknown) stored in @col, I need check its value, but 'select @col' not work, it just show ExtraField. Anybody give me direction. Thanks
 
Try this
Code:
EXECUTE( "select " + @col + " FROM aTable WHERE whatever" )
 
If I am reading you correctly you want to dynamicly add columns to the select statement..

This cant be done without dynamic execution..

i.e.

(important stuff in red)
Code:
-- This works...
Declare @col varchar(300)
select @col = 'orderid'
[red]exec ('[/red]select [red]' + @col + '[/red] from northwind.dbo.orders[red]')[/red]
go
-- THis doesn't
Declare @col varchar(300)
select @col = 'orderid'
select @col from northwind.dbo.orders
 
Thanks friends.
Your solutions work fine. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top