[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Determine column names of dynamic query result set

Posted on 2004-04-06
5
Medium Priority
?
295 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 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

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
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…
Suggested Courses

872 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