DECLARE str VARCHAR2(50);
text VARCHAR2(100);
CURSOR cur is SELECT DISTINCT time_period
FROM s1.TIME_PERIOD_LOOKUP
WHERE period_type = 'Q'
ORDER BY time_period;
BEGIN
OPEN cur;
LOOP
FETCH cur into str;
EXIT WHEN cur%NOTFOUND;
text:= text || '''' || str || '''' || ',';
END LOOP;
CLOSE cur;
text:= substr(text, 0, length(text)-1);
dbms_output.put_line(text);
END;
SELECT text as Text FROM dual; /* Need to output the 'text' variable via a SELECT statement*/
SQL> select * from time_period_lookup;
TIME_PERIOD PE
-------------------- --
10 Q
50 Q
60 T
20 Q
SQL> create or replace function test_func
2 return varchar2
3 as
4 str VARCHAR2(50);
5 text VARCHAR2(100);
6 CURSOR cur is SELECT DISTINCT time_period
7 FROM TIME_PERIOD_LOOKUP
8 WHERE period_type = 'Q'
9 ORDER BY time_period;
10 begin
11 OPEN cur;
12 LOOP
13 FETCH cur into str;
14 EXIT WHEN cur%NOTFOUND;
15 text:= text || '''' || str || '''' || ',';
16 END LOOP;
17
18 CLOSE cur;
19
20 text := rtrim(text, ',');
21
22 return text;
23 end;
24 /
Function created.
SQL> select test_func as text from dual;
TEXT
--------------------------------------------------------
'10','20','50'
SQL>
ASKER
ASKER
Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.
TRUSTED BY
Hope this helps.
http://it.toolbox.com/blogs/oracle-guide/learn-plsql-procedures-and-functions-13030