Solved

Determine column names of dynamic query result set

Posted on 2004-04-06
5
286 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 SQL - Query Missing Value 10 67
MS Access 2003 Query 2 51
Insert with SET how to handle join 6 57
Process mapping 5 27
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

896 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

16 Experts available now in Live!

Get 1:1 Help Now