Unix Technical Forum

Retrieving column names in the result set

This is a discussion on Retrieving column names in the result set within the Sybase forums, part of the Database Server Software category; --> Hi, I've been looking at the documentation, but thougth I'd ask you to maybe save time. Say I have ...


Go Back   Unix Technical Forum > Database Server Software > Sybase

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 04:04 PM
Amy
 
Posts: n/a
Default Retrieving column names in the result set

Hi,

I've been looking at the documentation, but thougth I'd ask you to maybe save time.

Say I have a temp table with the following columns
create table #person (
person_id int,
last_name varchar(10),
first_name varchar(10),
ssn varchar(11))

say I stuffed a bunch of data in it
1 Schuele Amy 12345678901
2 Quinn Steve 23456789012

what I want as my result set is: (the order is not important)
1, last_name, Schuele
1, first_name, Amy
1, ssn, 12345678901
2, last_name, Quinn
2, first_name, Steve
2, ssn, 23456789012

so, I want the column name as part of the result set, returned as a string.

Obviously I'm trying to get the data out into (id) name:value pairs for some
processing down the line.

Thanks for any ideas!
Amy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 04:04 PM
Carl Kayser
 
Posts: n/a
Default Re: Retrieving column names in the result set

I don't know what you mean by "the order is not important". Does it mean
that the output order could be random? A simple union should do the trick:

select person_id, 'last_name', last_name
from #person
union
select person_id, 'first_name', first_name
from #person
union
select person_id, 'ssn', ssn
from #person
order by person_id

"Amy" <aschuele@ncsa.uiuc.edu> wrote in message
news:6d1ab15.0311061333.71b63118@posting.google.co m...
> Hi,
>
> I've been looking at the documentation, but thougth I'd ask you to maybe

save time.
>
> Say I have a temp table with the following columns
> create table #person (
> person_id int,
> last_name varchar(10),
> first_name varchar(10),
> ssn varchar(11))
>
> say I stuffed a bunch of data in it
> 1 Schuele Amy 12345678901
> 2 Quinn Steve 23456789012
>
> what I want as my result set is: (the order is not important)
> 1, last_name, Schuele
> 1, first_name, Amy
> 1, ssn, 12345678901
> 2, last_name, Quinn
> 2, first_name, Steve
> 2, ssn, 23456789012
>
> so, I want the column name as part of the result set, returned as a

string.
>
> Obviously I'm trying to get the data out into (id) name:value pairs for

some
> processing down the line.
>
> Thanks for any ideas!
> Amy



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 04:04 PM
Amy Schuele
 
Posts: n/a
Default Re: Retrieving column names in the result set

Thanks for the suggestion.

What I should have pointed out is that this is a small example. My real
table has many more columns, including address, phone, etc. So, while
this will work it would be cumbersome. I was hoping to find a solution
that doesn't require as many lines of code.

Thanks again!
Amy

"Carl Kayser" <kayser_c@bls.gov> wrote in message
news:bog1pr$j21$1@blsnews.bls.gov...
> I don't know what you mean by "the order is not important". Does it mean
> that the output order could be random? A simple union should do the

trick:
>
> select person_id, 'last_name', last_name
> from #person
> union
> select person_id, 'first_name', first_name
> from #person
> union
> select person_id, 'ssn', ssn
> from #person
> order by person_id
>
> "Amy" <aschuele@ncsa.uiuc.edu> wrote in message
> news:6d1ab15.0311061333.71b63118@posting.google.co m...
> > Hi,
> >
> > I've been looking at the documentation, but thougth I'd ask you to maybe

> save time.
> >
> > Say I have a temp table with the following columns
> > create table #person (
> > person_id int,
> > last_name varchar(10),
> > first_name varchar(10),
> > ssn varchar(11))
> >
> > say I stuffed a bunch of data in it
> > 1 Schuele Amy 12345678901
> > 2 Quinn Steve 23456789012
> >
> > what I want as my result set is: (the order is not important)
> > 1, last_name, Schuele
> > 1, first_name, Amy
> > 1, ssn, 12345678901
> > 2, last_name, Quinn
> > 2, first_name, Steve
> > 2, ssn, 23456789012
> >
> > so, I want the column name as part of the result set, returned as a

> string.
> >
> > Obviously I'm trying to get the data out into (id) name:value pairs for

> some
> > processing down the line.
> >
> > Thanks for any ideas!
> > Amy

>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 04:04 PM
Nuggy
 
Posts: n/a
Default Re: Retrieving column names in the result set

aschuele@ncsa.uiuc.edu (Amy) wrote in message news:<6d1ab15.0311061333.71b63118@posting.google.c om>...
> Hi,
>
> I've been looking at the documentation, but thougth I'd ask you to maybe save time.
>
> Say I have a temp table with the following columns
> create table #person (
> person_id int,
> last_name varchar(10),
> first_name varchar(10),
> ssn varchar(11))
>
> say I stuffed a bunch of data in it
> 1 Schuele Amy 12345678901
> 2 Quinn Steve 23456789012
>
> what I want as my result set is: (the order is not important)
> 1, last_name, Schuele
> 1, first_name, Amy
> 1, ssn, 12345678901
> 2, last_name, Quinn
> 2, first_name, Steve
> 2, ssn, 23456789012
>
> so, I want the column name as part of the result set, returned as a string.
>
> Obviously I'm trying to get the data out into (id) name:value pairs for some
> processing down the line.
>
> Thanks for any ideas!
> Amy



As long as you don't care about the order of the results, you could
try something like the following:

select person_id, "last_name", last_name from #person
select person_id, "first_name", first_name from #person
select person_id, "ssn", ssn from #person
go

The only drawback is the need to manually specify each column you want
with a seperate select line. It's possible to retrieve column names
using the sysobjects and syscolumns tables, but to use them to do what
you want, you'd probably need to use a cursor, and even then I'm not
aware of being able to do selects by passing dynamic column names into
a single select statement.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 04:05 PM
Navaneet Chola
 
Posts: n/a
Default Re: Retrieving column names in the result set

Dear Amy

Just try it out

select person_id,",last_name,",last_name+char10)
,person_id,",last_name,",last_name+char(10)
, person_id,",ssn",ssn
from #person

I hope your problem must be solved.

Navneet

"Amy Schuele" <aschuele@ANTISPAM.ncsa.uiuc.edu> wrote in message news:<muOqb.137933$Fm2.114575@attbi_s04>...
> Thanks for the suggestion.
>
> What I should have pointed out is that this is a small example. My real
> table has many more columns, including address, phone, etc. So, while
> this will work it would be cumbersome. I was hoping to find a solution
> that doesn't require as many lines of code.
>
> Thanks again!
> Amy
>
> "Carl Kayser" <kayser_c@bls.gov> wrote in message
> news:bog1pr$j21$1@blsnews.bls.gov...
> > I don't know what you mean by "the order is not important". Does it mean
> > that the output order could be random? A simple union should do the

> trick:
> >
> > select person_id, 'last_name', last_name
> > from #person
> > union
> > select person_id, 'first_name', first_name
> > from #person
> > union
> > select person_id, 'ssn', ssn
> > from #person
> > order by person_id
> >
> > "Amy" <aschuele@ncsa.uiuc.edu> wrote in message
> > news:6d1ab15.0311061333.71b63118@posting.google.co m...
> > > Hi,
> > >
> > > I've been looking at the documentation, but thougth I'd ask you to maybe

> save time.
> > >
> > > Say I have a temp table with the following columns
> > > create table #person (
> > > person_id int,
> > > last_name varchar(10),
> > > first_name varchar(10),
> > > ssn varchar(11))
> > >
> > > say I stuffed a bunch of data in it
> > > 1 Schuele Amy 12345678901
> > > 2 Quinn Steve 23456789012
> > >
> > > what I want as my result set is: (the order is not important)
> > > 1, last_name, Schuele
> > > 1, first_name, Amy
> > > 1, ssn, 12345678901
> > > 2, last_name, Quinn
> > > 2, first_name, Steve
> > > 2, ssn, 23456789012
> > >
> > > so, I want the column name as part of the result set, returned as a

> string.
> > >
> > > Obviously I'm trying to get the data out into (id) name:value pairs for

> some
> > > processing down the line.
> > >
> > > Thanks for any ideas!
> > > Amy

> >
> >

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:52 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