Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Retrieve Columns

Author  Topic 

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-09 : 06:10:02
Is there a query that will retrun column names as seperate columns?
I have a table test having the columns id, name and spec
I can write a query

Select name from syscolumns order by colorder

but the output is
id
name
spec

but my desired output woulld be
Col1 Col2 Col3
id name spec


Madhivanan

Failing to plan is Planning to fail

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-09 : 10:26:11
Why?

Do you want separate columns or a delimted srring?



Brett

8-)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-10 : 00:31:33
Brett, I want seperate columns not delimited string

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-05-10 : 08:57:56
Why do you want this? what are doing with this data? Your presentation layer can easily determine the columns from a recordset and display them any way you want, if your goal is just to list/present these columns. A brief paragraph about what tools you are using and what your goal is might be really helpful here, it sounds like you are making things more complicated then they need to be.

- Jeff
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-10 : 09:04:53
Jeff, Actually I want to export the data to Excel file
From QA, I run query and results are displaying in the Grid. When I copy the contents of Grid and paste in Excel, Column names are not copied. Thats why I wanted to do this

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-05-10 : 09:11:31
see how you are overcomplicating things to accomodate a simple cut and paste?

here's 3 ways to do this quite easily:

1) don't use the grid in query analyzer, use the text format for you results. Run the SQL, copy all of the results, paste into Excel and when they are all still highlighted use Data->Text to Columns and choose Fixed width. This turns your large 1 column text result into multiple columns quite well, with headers.

2) Another idea is to use MS Access as a middle-man between SQL and Excel -- it works great and query results in Access paste into Excel quite nicely. Create view of your SQL in SQL Server and link to it in Access. Or use a pass-through query in Access that links directly into SQL Server for ad-hoc SQL execution. The results are in a nice grid that is easily pasted into Excel.

3) if you do this often, you can create a view and link directly into SQL Server from Excel, look at Data-->Get External Data. Or you can export from SQL Server using DTS into an Excel file.

Does this help?



- Jeff
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-10 : 09:37:47
yes, this really helps. Thanks Jeff

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-05-10 : 09:55:45
If You use these options: (QA->Options->Results)
"Results To Text"
"Tab Delimited"

Then the result will copy straight in to Excel, with column headers an' all.

rockmoose
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-10 : 10:01:02
rockmoose, Yours also good idea

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-10 : 10:09:49
Then there's always the hard way...just cut and paste the code and you're good to go

http://weblogs.sqlteam.com/brettk/archive/2005/04/13/4395.aspx



Brett

8-)
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-05-10 : 10:18:59
>> Then there's always the hard way...just cut and paste the code and you're good to go
I knew, there had to be a harder way.

rockmoose
Go to Top of Page
   

- Advertisement -