Solved

Determine column names of dynamic query result set

Posted on 2004-04-06
5
288 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
  • 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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

831 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