在Oracle中创建一个函数,本来是想返回一个index table的,没有成功。想到文本也可以传输信息,就突然来了灵感,把返回值设置文本格式。
考虑到返回数据量可能会很大,varchar2类型长度吃紧,于是将返回值类型设置为clob。
我是用scott用户的测试表emp,这个是函数定义情况:
1 create or replace function test_query_func(dept varchar2)
2 return clob
3 is
4 type test_record is record
5 (rec_empno emp.empno%type,
6 rec_ename emp.ename%type,
7 rec_job emp.job%type,
8 rec_sal emp.sal%type);
9 type test_query_arr is table of test_record index by binary_integer;
10 cursor cur is select empno, ename, job, sal from emp where deptno = dept;
11 test_query test_query_arr;
12 i integer := 0;
13 ss varchar2(200) := '';
14 res clob := '[';
15 begin
16 for c in cur loop
17 i := i + 1;
18 test_query(i) := c;
19 end loop;
20 for q in 1..test_query.count loop
21 ss := '(''' || test_query(q).rec_empno || ''', ''' || test_query(q).rec_ename || ''', ''' || test_query(q).rec_job || ''', ''' || test_query(q).rec_sal || ''')';
22 if q