Link to home
Start Free TrialLog in
Avatar of nixj14
nixj14

asked on

Variable column names

Is it possible to use a variable column name?  I realize that you can call execute to execute the command, but execute does not return a resultset, so it becomes a little bit hairier than I would have liked it to be.  I'm using SQL Server 7.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

<<but execute does not return a resultset>>
I think you will find it can.

Anthony
AFAIK the only way to vary what a column name will be/what columns appear in a query is to use dynamic SQL as you mentioned, either:

EXEC(sql)

or

EXEC sp_executesql @sql

where @sql has been constructed to contain the column(s) you want.
Avatar of nixj14
nixj14

ASKER

acperkins, how does it return a resultset?  I'm not seeing documentation for this
Just as Scott has pointed out:

Exec('Select * from Customers')

Will give you a resultset.

Anthony
Avatar of nixj14

ASKER

That returns the resultset to a screen, but it doesn't make a resultset usable in SQL.  For instance:

say I had a the following query:

select * from customers
where cg_id in (put execute statement here)

it doesn't work when I tried it
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The use of the NorthWind database was just as an example as I am unfamiliar with your data structure. Using this database makes it simple for you to check yourself.

Anthony
Uh, thanks.

Anthony