Solved

Variable column names

Posted on 2002-05-24
8
201 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
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

810 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