?
Solved

Determine column names of dynamic query result set

Posted on 2004-04-06
5
Medium Priority
?
292 Views
Last Modified: 2012-06-21
Hello,

I need to get the column names (and hopefully datatypes) of a result set generated by a dynamic query.  The query is stored in a varchar variable.  For example:

DECLARE @query VARCHAR(1024);
set @query = 'Select * from my_table'

Since @query could be anything, including a result set based on multiple joins, I have no way of determining column names from SYSCOLUMNS.  

Ideally, I would like a result set of column names (in a single column).

Thanks!
0
Comment
Question by:quiTech
[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
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:plq
ID: 10770754
Whats your client ? This is easy with ADO
0
 

Author Comment

by:quiTech
ID: 10770890
I can do this with ADO without problems, and this will be my "fall-back" option.  However, I would prefer to do it in TransactSQL as a stored procedure, so I don't have to re-write it for different clients.
0
 
LVL 8

Accepted Solution

by:
plq earned 2000 total points
ID: 10772330
The only way I can think of is to execute into a temp table and then use syscolumns

You don't have to put any data across, so it should be fast. I tested the following and it seems OK. Except that if you've already got a where clause you'll have to replace instead of append. Oh and if the word ' from ' occurs in a constant in your select clause you'll get a problem too !

declare @query varchar(255)
drop table tempdb..temp
select @query = 'select * from mytable'
select @query = replace(@query, ' from ', 'into tempdb..temp from ') + ' where 0 = 1 '
exec(@query)

select a.name from tempdb..syscolumns a inner join tempdb..sysobjects b on a.id = b.id
where b.name = 'temp'


0
 

Author Comment

by:quiTech
ID: 10776546
Works great!  Thanks!
0
 
LVL 8

Expert Comment

by:plq
ID: 10778192
my pleasure. Don't forget this wouldn't be multi-user, due to the temp table name

cheers!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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