我想打印的数据Cursor_pkg.c1.row_emp,例如:在程序正常工作后将Cursor_pkg.c1.row_emp.last_name存在该数据。我该怎么做?Cursor_pkg.row_emp``Cursor_pkg_func.Print_Cur
Cursor_pkg.c1.row_emp
Cursor_pkg.c1.row_emp.last_name
Cursor_pkg.row_emp``Cursor_pkg_func.Print_Cur
有两个问题:我想从包emp_rec(行)中输出数据,我想直接从PACKAGECursor_pkg_func过程PS中输出数据。主要思想是存储数据以及用于获取和选择数据的过程/函数
emp_rec
Cursor_pkg_func
CREATE OR REPLACE PACKAGE Cursor_pkg AUTHID DEFINER IS CURSOR C1 IS SELECT last_name, job_id FROM employees WHERE job_id LIKE '%CLERK%' AND manager_id > 120 ORDER BY last_name; row_emp C1%ROWTYPE; END Cursor_pkg; / CREATE OR REPLACE PACKAGE Cursor_pkg_func IS PROCEDURE Print_Cur; END Cursor_pkg_func; / CREATE OR REPLACE PACKAGE BODY Cursor_pkg_func IS PROCEDURE Print_Cur IS BEGIN OPEN Cursor_pkg.C1; LOOP FETCH Cursor_pkg.C1 INTO Cursor_pkg.row_emp; EXIT when Cursor_pkg.C1%NOTFOUND; DBMS_OUTPUT.put_line(Cursor_pkg.row_emp.last_name); END LOOP; CLOSE Cursor_pkg.C1; END; END; / BEGIN Cursor_pkg_func.Print_Cur; END;
但是我想从Cursor_pkg.row_emp PACKAGE中选择并打印,而无需创建函数。以及如何不仅打印last_name而是打印所有行?错误始于:最后三个语句出了什么问题?
CREATE OR REPLACE PACKAGE Cursor_pkg_func IS TYPE outrec_typ IS RECORD ( var_char2 VARCHAR2(30) ); TYPE outrecset IS TABLE OF outrec_typ; FUNCTION f_trans (p in number ) RETURN outrecset PIPELINED; END Cursor_pkg_func; / CREATE OR REPLACE PACKAGE BODY Cursor_pkg_func IS FUNCTION f_trans (p in number) RETURN outrecset PIPELINED IS out_rec outrec_typ; BEGIN OPEN Cursor_pkg.C1; LOOP FETCH Cursor_pkg.C1 INTO Cursor_pkg.row_emp; EXIT when Cursor_pkg.C1%NOTFOUND; END LOOP; LOOP out_rec.var_char2 := Cursor_pkg.row_emp.last_name; PIPE ROW(out_rec); DBMS_OUTPUT.put_line(out_rec.var_char2); END LOOP; CLOSE Cursor_pkg.C1; RETURN; END f_trans; END Cursor_pkg_func; / begin Cursor_pkg_func.f_trans(5); end; /
您已经定义了流水线函数,这不是调用它的方法:
SQL> begin 2 Cursor_pkg_func.f_trans(5); 3 end; 4 / Cursor_pkg_func.f_trans(5); * ERROR at line 2: ORA-06550: line 2, column 1: PLS-00221: 'F_TRANS' is not a procedure or is undefined ORA-06550: line 2, column 1: PL/SQL: Statement ignored SQL>
您需要使用TABLE()函数。虽然这样您会发现代码中的错误:
SQL> select * from table(Cursor_pkg_func.f_trans(5)) 2 / SMITH SMITH SMITH '''' SMITH SMITH SMITH SMITH ERROR: ORA-00028: your session has been killed 273660 rows selected. SQL>
请注意,我必须从另一个会话中终止该会话,否则它将仍在运行。因此,让我们简化函数并摆脱那毫无意义的第二个循环…。
CREATE OR REPLACE PACKAGE BODY Cursor_pkg_func IS FUNCTION f_trans (p in number) RETURN outrecset PIPELINED IS out_rec outrec_typ; BEGIN OPEN Cursor_pkg.C1; LOOP FETCH Cursor_pkg.C1 INTO Cursor_pkg.row_emp; EXIT when Cursor_pkg.C1%NOTFOUND; out_rec.var_char2 := Cursor_pkg.row_emp.last_name; PIPE ROW(out_rec); END LOOP; CLOSE Cursor_pkg.C1; RETURN; END f_trans; END Cursor_pkg_func; /
....然后瞧!
SQL> select * from table(Cursor_pkg_func.f_trans(5)) 2 / VAR_CHAR2 ------------------------------ ADAMS JAMES MILLER SMITH SQL>
“当我添加开始和结束时;选择不起作用”
您已经创建了流水线函数。你为什么这么做?之所以这样做,是因为您想要一个可以在SELECT语句的FROM子句中使用的PL / SQL函数。这就是流水线函数的用例。因此,将调用放入匿名PL / SQL块中实际上没有任何意义。
但不管怎么说。
请阅读文档。它是非常全面的,它是在线免费的。《 PL / SQL参考》中的相关部分是“静态SQL”一章。很明显,PL / SQL中的SELECT语句必须 始终将 记录提取到具有某些描述的变量中。在这方面,匿名PL / SQL块与存储过程相同。 了解更多。