?
Solved

Variable column names

Posted on 2002-05-24
8
Medium Priority
?
226 Views
Last Modified: 2008-01-16
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
Comment
Question by:nixj14
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7033183
<<but execute does not return a resultset>>
I think you will find it can.

Anthony
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 7033194
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
 
LVL 1

Author Comment

by:nixj14
ID: 7033217
acperkins, how does it return a resultset?  I'm not seeing documentation for this
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7033245
Just as Scott has pointed out:

Exec('Select * from Customers')

Will give you a resultset.

Anthony
0
 
LVL 1

Author Comment

by:nixj14
ID: 7033276
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
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 100 total points
ID: 7033311
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7033408
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7047076
Uh, thanks.

Anthony
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question