Unix Technical Forum

Output Column names in each Row along with the row Value

This is a discussion on Output Column names in each Row along with the row Value within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I was wondering if anyone can help me figure something out. Is it possible to do a querey ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:24 PM
Yas
 
Posts: n/a
Default Output Column names in each Row along with the row Value

Hello,

I was wondering if anyone can help me figure something out.
Is it possible to do a querey in MS SQL server and have the results
returned so that each result in each row is preceeded by the column
name?

eg. instead of usual output -> colName1, colValue1,
colName2,colValue2,colName3,colValue3 ?


Also I would like to only have this for certain columns ie in the
above example only for columns 2 and 3


Thank you! :-)

Yas

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:24 PM
M A Srinivas
 
Posts: n/a
Default Re: Output Column names in each Row along with the row Value

On Jun 29, 3:09 pm, Yas <yas...@gmail.com> wrote:
> Hello,
>
> I was wondering if anyone can help me figure something out.
> Is it possible to do a querey in MS SQL server and have the results
> returned so that each result in each row is preceeded by the column
> name?
>
> eg. instead of usual output -> colName1, colValue1,
> colName2,colValue2,colName3,colValue3 ?
>
> Also I would like to only have this for certain columns ie in the
> above example only for columns 2 and 3
>
> Thank you! :-)
>
> Yas


Not sure what you are actually looking at

create table #temp (col1 int, col2 int , col3 varchar(10))
insert into #temp values (1,10,'abcd')
insert into #temp values (2,20,'efgh')


select col1, ' col2:'+cast(col2 as varchar(10))+ '-col3:'+col3 as
col23
from #temp

drop table #temp

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:24 PM
Yas
 
Posts: n/a
Default Re: Output Column names in each Row along with the row Value

On 29 Jun, 14:27, M A Srinivas <masri...@gmail.com> wrote:
> On Jun 29, 3:09 pm, Yas <yas...@gmail.com> wrote:
>
>
>
> > Hello,

>
> > I was wondering if anyone can help me figure something out.
> > Is it possible to do a querey in MS SQL server and have the results
> > returned so that each result in each row is preceeded by the column
> > name?

>
> > eg. instead of usual output -> colName1, colValue1,
> > colName2,colValue2,colName3,colValue3 ?

>
> > Also I would like to only have this for certain columns ie in the
> > above example only for columns 2 and 3

>
> > Thank you! :-)

>
> > Yas

>
> Not sure what you are actually looking at
>


Hi, thanks for the response. Basically I want to have a query in a DTS
package that outputs to a text file with each line containing 1 row of
results.
What I would like to have is on each line before a given value is
written the column header for that value is also written. the file is
a semicolon delimmeted file so it would look something like this....

col1Name;value1;col2Name;value2;col3Name;value3... .etc

so...
FirstName;firstNameValue;SecondName;secondNameValu e;Occupation;occupationValue..etc

Now I can just do...
SELECT 'FirstName', firstName, 'SecondName', secondName,
'Occupation' occupation
FROM Table1
Which would output the table I want with the values between '' showing
up in unnamed columns.... BUT is there a better way of doing this?
especially as I want to do this in a DTS package Export Data which
esentially has a create table type command to ouptut to a text file
and throws errors that destination column name not defined.

Thanks :-)


> create table #temp (col1 int, col2 int , col3 varchar(10))
> insert into #temp values (1,10,'abcd')
> insert into #temp values (2,20,'efgh')
>
> select col1, ' col2:'+cast(col2 as varchar(10))+ '-col3:'+col3 as
> col23
> from #temp
>
> drop table #temp



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 03:24 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Output Column names in each Row along with the row Value

Yas (yasar1@gmail.com) writes:
> Now I can just do...
> SELECT 'FirstName', firstName, 'SecondName', secondName,
> 'Occupation' occupation
> FROM Table1
> Which would output the table I want with the values between '' showing
> up in unnamed columns.... BUT is there a better way of doing this?


That would indeed be the way to do it.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 03:24 PM
Yas
 
Posts: n/a
Default Re: Output Column names in each Row along with the row Value

On 29 Jun, 23:38, Erland Sommarskog <esq...@sommarskog.se> wrote:
> Yas (yas...@gmail.com) writes:
> > Now I can just do...
> > SELECT 'FirstName', firstName, 'SecondName', secondName,
> > 'Occupation' occupation
> > FROM Table1
> > Which would output the table I want with the values between '' showing
> > up in unnamed columns.... BUT is there a better way of doing this?

>
> That would indeed be the way to do it.


Thanks! :-)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:18 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com