Solved

Variable column names

Posted on 2002-05-24
8
221 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift 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 50 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 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