我正在编写带有管道功能的Oracle软件包以获取多个记录,该查询涉及多个联接的表,这些表将保存到另一个表中,我创建了 TYPE RECORD 和< strong> TABLE TYPE 作为类型记录,然后我用他们的查询创建了流水线函数,但是当我编译软件包时,出现了错误“表达式类型错误” 。 >
以下是定义包:
CREATE OR REPLACE PACKAGE MYPACKAGE_PKG AS
TYPE GLD_R IS RECORD (
ID NUMBER,
PRCCVE NUMBER(7,0),
LOTCVE NUMBER(7,0),
EPCSEQ NUMBER(18,0),
EPCBNK CHAR(3)
);
TYPE GLD_T IS TABLE OF GLD_R;
FUNCTION MY_FUNCTION(_NUM NUMBER) RETURN GLD_T PIPELINED;
END;
这是尸体包装:
CREATE OR REPLACE PACKAGE BODY MYPACKAGE_PKG AS
FUNCTION MY_FUNCTION(_NUM NUMBER) RETURN GLD_T PIPELINED
AS
CURSOR T_CUR IS
SELECT
T1.ID,
T2.COLUMN01,
T2.COLUMN02,
T3.COLUMN01,
T3.COLUMN02
FROM
TABLE01 T1
INNER JOIN TABLE02 T2 ON
T1.COLUMN03 = T2.ID
INNER JOIN TABLE03 T3 ON
T1.COLUMN04 = T2.ID
WHERE
T1.COLUMN01 = _NUM
BEGIN
FOR REC IN T_CUR LOOP
PIPE ROW (REC);
END LOOP;
END MY_FUNCTION;
END;
你能说我,我在做错什么吗?
答案 0 :(得分:1)
应该是这样的:
首先使用表(这样编译不会失败):
SQL> create table table01 (id number,column01 number, column03 number, column04 number);
Table created.
SQL> create table table02 (id number, column01 number, column02 number);
Table created.
SQL> create table table03 (id number, column01 number, column02 number);
Table created.
包装规格:
SQL> CREATE OR REPLACE PACKAGE MYPACKAGE_PKG AS
2
3 TYPE GLD_R IS RECORD (
4 ID NUMBER,
5 PRCCVE NUMBER(7,0),
6 LOTCVE NUMBER(7,0),
7 EPCSEQ NUMBER(18,0),
8 EPCBNK CHAR(3)
9 );
10
11 TYPE GLD_T IS TABLE OF GLD_R;
12
13 FUNCTION MY_FUNCTION(p_NUM NUMBER) RETURN GLD_T PIPELINED;
14
15 END;
16 /
Package created.
包装体:
SQL> CREATE OR REPLACE PACKAGE BODY MYPACKAGE_PKG AS
2
3 FUNCTION MY_FUNCTION(p_NUM NUMBER) RETURN GLD_T PIPELINED
4 AS
5 CURSOR T_CUR IS
6 SELECT
7 T1.ID,
8 T2.COLUMN01 t2c01,
9 T2.COLUMN02 t2c02,
10 T3.COLUMN01 t3c01,
11 T3.COLUMN02 t3c02
12 FROM
13 TABLE01 T1
14 INNER JOIN TABLE02 T2 ON
15 T1.COLUMN03 = T2.ID
16 INNER JOIN TABLE03 T3 ON
17 T1.COLUMN04 = t3.id -- not T2.ID
18 WHERE
19 T1.COLUMN01 = p_NUM;
20
21 myrec gld_r;
22 BEGIN
23 FOR REC IN T_CUR LOOP
24 myrec.id := rec.id;
25 myrec.prccve := rec.t2c01;
26 myrec.lotcve := rec.t2c02;
27 myrec.epcseq := rec.t3c01;
28 myrec.epcbnk := rec.t3c02;
29 PIPE ROW (myrec);
30 END LOOP;
31 END MY_FUNCTION;
32
33 END;
34 /
Package body created.
SQL>