是否可以使用 rowtype 将游标提取到记录中?
Is it possible to fetch a cursor into a record using rowtype?
我总是得到
PLS-00597: expression 'V_EMP_REC' in the INTO list is of wrong type
这是包,我使用的是默认 HR 模式
CREATE OR REPLACE PACKAGE EMP_PKG AS
TYPE T_EMP_REC IS RECORD (
V_EMP_TAB EMPLOYEES%ROWTYPE,
V_DEPT_NAME DEPARTMENTS.DEPARTMENT_NAME%TYPE
);
FUNCTION GET_EMP (P_EMP_ID EMPLOYEES.EMPLOYEE_ID%TYPE) RETURN T_EMP_REC;
END EMP_PKG;
/
CREATE OR REPLACE PACKAGE BODY EMP_PKG AS
FUNCTION GET_EMP (P_EMP_ID EMPLOYEES.EMPLOYEE_ID%TYPE)
RETURN T_EMP_REC
AS
CURSOR V_EMP_CUR IS
SELECT E.*, D.DEPARTMENT_NAME
FROM EMPLOYEES E, DEPARTMENTS D
WHERE E.EMPLOYEE_ID = P_EMP_ID
AND E.DEPARTMENT_ID = D.DEPARTMENT_ID;
V_EMP_REC T_EMP_REC;
BEGIN
OPEN V_EMP_CUR;
FETCH V_EMP_CUR INTO V_EMP_REC;
CLOSE V_EMP_CUR;
RETURN V_EMP_REC;
END GET_EMP;
END EMP_PKG;
列数匹配,我总是使用与 table 相同的类型。我不知道这种方法是否可行,而不是在记录类型中声明每一列。
您必须明确声明集合中 employees
和 departments
的所有列:
TYPE t_emp_rec IS RECORD (employees_col1 employees.employees_col1%TYPE,
employees_col2 employees.employees_col2%TYPE,
...
departments_col1 departments.departments_col1%TYPE,
departments_col2 departments.departments_col2%TYPE
...
);
在您的代码中出现错误,因为您正在尝试将列类型(NUMBER、VARCHAR2、DATE...)分配给 RECORD 集合。
正如 Guillaume 所建议的,明确提及列名。当您调用该函数时,您会收到 T_EMP_REC 记录。现在每个变量都可以访问为 T_EMP_REC.employees_col1, T_EMP_REC.employees_col2, T_EMP_REC.department_col1 等
无法回复您对他的回答的评论,所以这样回答。
作为替代方案,如果您只希望记录类型是平面结构并与光标匹配,您可以将光标放在包规范中并将类型定义为 cursor%rowtype
:
create or replace package emp_pkg
as
cursor emp_cur
( cp_emp_id employees.employee_id%type )
is
select e.*, d.department_name
from employees e
join departments d
on e.department_id = d.department_id
where e.employee_id = cp_emp_id;
subtype t_emp_rec is emp_cur%rowtype;
function get_emp
( p_emp_id employees.employee_id%type )
return t_emp_rec;
end emp_pkg;
create or replace package body emp_pkg
as
function get_emp
( p_emp_id employees.employee_id%type )
return t_emp_rec
is
v_emp_rec t_emp_rec;
begin
open emp_cur(p_emp_id);
fetch emp_cur into v_emp_rec;
close emp_cur;
return v_emp_rec;
end get_emp;
end emp_pkg;
我总是得到
PLS-00597: expression 'V_EMP_REC' in the INTO list is of wrong type
这是包,我使用的是默认 HR 模式
CREATE OR REPLACE PACKAGE EMP_PKG AS
TYPE T_EMP_REC IS RECORD (
V_EMP_TAB EMPLOYEES%ROWTYPE,
V_DEPT_NAME DEPARTMENTS.DEPARTMENT_NAME%TYPE
);
FUNCTION GET_EMP (P_EMP_ID EMPLOYEES.EMPLOYEE_ID%TYPE) RETURN T_EMP_REC;
END EMP_PKG;
/
CREATE OR REPLACE PACKAGE BODY EMP_PKG AS
FUNCTION GET_EMP (P_EMP_ID EMPLOYEES.EMPLOYEE_ID%TYPE)
RETURN T_EMP_REC
AS
CURSOR V_EMP_CUR IS
SELECT E.*, D.DEPARTMENT_NAME
FROM EMPLOYEES E, DEPARTMENTS D
WHERE E.EMPLOYEE_ID = P_EMP_ID
AND E.DEPARTMENT_ID = D.DEPARTMENT_ID;
V_EMP_REC T_EMP_REC;
BEGIN
OPEN V_EMP_CUR;
FETCH V_EMP_CUR INTO V_EMP_REC;
CLOSE V_EMP_CUR;
RETURN V_EMP_REC;
END GET_EMP;
END EMP_PKG;
列数匹配,我总是使用与 table 相同的类型。我不知道这种方法是否可行,而不是在记录类型中声明每一列。
您必须明确声明集合中 employees
和 departments
的所有列:
TYPE t_emp_rec IS RECORD (employees_col1 employees.employees_col1%TYPE,
employees_col2 employees.employees_col2%TYPE,
...
departments_col1 departments.departments_col1%TYPE,
departments_col2 departments.departments_col2%TYPE
...
);
在您的代码中出现错误,因为您正在尝试将列类型(NUMBER、VARCHAR2、DATE...)分配给 RECORD 集合。
正如 Guillaume 所建议的,明确提及列名。当您调用该函数时,您会收到 T_EMP_REC 记录。现在每个变量都可以访问为 T_EMP_REC.employees_col1, T_EMP_REC.employees_col2, T_EMP_REC.department_col1 等
无法回复您对他的回答的评论,所以这样回答。
作为替代方案,如果您只希望记录类型是平面结构并与光标匹配,您可以将光标放在包规范中并将类型定义为 cursor%rowtype
:
create or replace package emp_pkg
as
cursor emp_cur
( cp_emp_id employees.employee_id%type )
is
select e.*, d.department_name
from employees e
join departments d
on e.department_id = d.department_id
where e.employee_id = cp_emp_id;
subtype t_emp_rec is emp_cur%rowtype;
function get_emp
( p_emp_id employees.employee_id%type )
return t_emp_rec;
end emp_pkg;
create or replace package body emp_pkg
as
function get_emp
( p_emp_id employees.employee_id%type )
return t_emp_rec
is
v_emp_rec t_emp_rec;
begin
open emp_cur(p_emp_id);
fetch emp_cur into v_emp_rec;
close emp_cur;
return v_emp_rec;
end get_emp;
end emp_pkg;