Solved

Determine column names of dynamic query result set

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Read about achieving the basic levels of HRIS security in the workplace.
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…

706 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

21 Experts available now in Live!

Get 1:1 Help Now