|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
select bookid, bookname, b.authorid, authorname from author a, Books b
where a.authorid=b.authorid; |
|
#3
|
|||
|
|||
|
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;
/
|
![]() |
| Viewing: Dev Shed Forums > Databases > Oracle Development > how to get comma seperated values in oracle |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|