Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 14th, 2005, 08:37 AM
vinod_mnr vinod_mnr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 vinod_mnr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 7 sec
Reputation Power: 0
how to get comma seperated values in oracle

Hello,

I have two tables in Oracle 9i
one is authors having the following fields:

AUTHOR
columnname datatype
-----------------------------------
authorid number
authorname varchar2


BOOKS
columnname datatype
-----------------------------------
bookid number
bookname varchar2
authorid varchar2


the values in AUTHOR table stores as follows:

authorid authorname
-----------------------------------
1 K.S Kashyap
2 K.S Rai
3 K.S sharma


the values in BOOKS table stores as follows:

bookid bookname authorid
------------------------------------------------
1 Oracle 1,2,3
2 .NET 2,3


the end result should be something like this, now what would be the query, so that i get this result



bookid bookname authorid authorname
--------------------------------------------------------------------------
1 Oracle 1,2,3 K.S Kashyap,K.S Rai,K.S sharma
2 .NET 2,3 K.S Rai,K.S sharma

help me to solve this problem in oracle 9i


Thanks in advance

Reply With Quote
  #2  
Old April 17th, 2005, 07:55 PM
paulh1983 paulh1983 is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2004
Posts: 1,239 paulh1983 User rank is Sergeant (500 - 2000 Reputation Level)paulh1983 User rank is Sergeant (500 - 2000 Reputation Level)paulh1983 User rank is Sergeant (500 - 2000 Reputation Level)paulh1983 User rank is Sergeant (500 - 2000 Reputation Level)paulh1983 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 25 m 28 sec
Reputation Power: 17
select bookid, bookname, b.authorid, authorname from author a, Books b
where a.authorid=b.authorid;

Reply With Quote
  #3  
Old April 22nd, 2005, 04:40 PM
wubandit99 wubandit99 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 30 wubandit99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 6 m 31 sec
Reputation Power: 4
Here is a simple function that will get you what you are looking for:


Code:
CREATE OR REPLACE FUNCTION get_list_text
(
	/*-- comma seperated list of ids --*/
	list_in IN VARCHAR2,
	/*-- table of id to text mapping --*/
	lkup_tbl_in IN VARCHAR2,
	/*-- column containing ids --*/
	key_col_in IN VARCHAR2,
	/*-- column containing text --*/
	desc_col_in IN VARCHAR2
)
return VARCHAR2
AUTHID CURRENT_USER
IS

	TYPE RowCurTyp IS REF CURSOR;
	lkup_desc_cur RowCurTyp;

	v_text_list VARCHAR2(3000);

	v_lkup_key NUMBER;
	v_lkup_desc VARCHAR2(32767);

BEGIN


	-- add commas to front and back of list so that first and last
	-- position are still surrounded by commas for replace search
	v_text_list := ','||list_in||',';

	OPEN lkup_desc_cur
	FOR '
		select '||key_col_in||' lkup_key, '||desc_col_in||' lkup_desc
		from '||lkup_tbl_in||'
		where '||key_col_in||' in ('||list_in||')';

	LOOP
      	FETCH lkup_desc_cur INTO v_lkup_key, v_lkup_desc;
		EXIT WHEN lkup_desc_cur%notfound;

		v_text_list := replace(v_text_list, 
                                             ','||v_lkup_key||',',
                                             ','|| v_lkup_desc||',');
				
	END LOOP;

	-- remove commas added as first step
	v_text_list := trim(BOTH ',' FROM v_text_list);

	CLOSE lkup_desc_cur;

	RETURN v_text_list;

END get_list_text;
/
Comments on this post
shammat agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > how to get comma seperated values in oracle


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway