Solved

Variable column names

Posted on 2002-05-24
8
190 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:ScottPletcher
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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nested cursor  in SQL 9 94
Sql Count with Select Distinct 4 26
Sql Join Problem 2 22
Extract string portion 2 10
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now