• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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.
0
nixj14
Asked:
nixj14
  • 5
  • 2
1 Solution
 
Anthony PerkinsCommented:
<<but execute does not return a resultset>>
I think you will find it can.

Anthony
0
 
Scott PletcherSenior DBACommented:
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.
0
 
nixj14Author Commented:
acperkins, how does it return a resultset?  I'm not seeing documentation for this
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Anthony PerkinsCommented:
Just as Scott has pointed out:

Exec('Select * from Customers')

Will give you a resultset.

Anthony
0
 
nixj14Author Commented:
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
0
 
Anthony PerkinsCommented:
True.  But you could do this using the NorthWind database:

Exec('Select * From Orders Where CustomerID In (Select CustomerID From Customers Where Country=''USA'')')

Anthony
0
 
Anthony PerkinsCommented:
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
0
 
Anthony PerkinsCommented:
Uh, thanks.

Anthony
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now