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.
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.
EXEC(sql)
or
EXEC sp_executesql @sql
where @sql has been constructed to contain the column(s) you want.
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
Exec('Select * from Customers')
Will give you a resultset.
Anthony
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Anthony
Uh, thanks.
Anthony
Anthony
I think you will find it can.
Anthony