This is function to convert values into comma separated string. This function takes input as cursor and joining delimiter and return a string. This code supports oracle 8 and above. One can test for lower versions also.
Please let me know if you face any problem.
Code Snippet
- CREATE OR REPLACE FUNCTION Fnc_JOIN
- (
- p_cursor sys_refcursor,
- p_del VARCHAR2 := ','
- ) RETURN VARCHAR2
- IS
- l_value VARCHAR2(32767);
- l_result VARCHAR2(32767);
- BEGIN
- LOOP
- FETCH p_cursor INTO l_value;
- EXIT WHEN p_cursor%NOTFOUND;
- IF l_result IS NOT NULL THEN
- l_result := l_result || p_del;
- END IF;
- l_result := l_result || l_value;
- END LOOP;
- RETURN l_result;
- END ;
How to call This function:
Code Snippet
- select fnc_join(cursor (SELECT emp_name FROM emp_table)) from dual;
One can change the concatenation delimiter to any other character as per requirement.
2 comments: