[英]How to Select multiple columns Into a table PL/SQL
I am trying to create a function with PL/SQL that return information about an employee, by getting the user input (code_Employe) and pass into this function. So the function get this code or ID and search for the employee with this ID.我正在尝试使用 PL/SQL 创建一个 function,它通过获取用户输入 (code_Employe) 并传递到此 function 来返回有关员工的信息。因此 function 获取此代码或 ID 并使用此 ID 搜索员工。
My problem is how that I don't know how to insert all the employee information in the return variable.我的问题是我不知道如何将所有员工信息插入返回变量中。 Do I have to create a temporary table?
我必须创建一个临时表吗?
Here is my code:这是我的代码:
CREATE OR REPLACE FUNCTION fNombreProjets(code_Emp IN Employe.idEmploye%type)
RETURN varchar2
is
info_Emp varchar2(200);
BEGIN
SELECT e.IDEMPLOYE ,
e.NOMEMPLOYE ,
e.ADRESSE ,
e.TELEPHONE ,
e.ADRESSECOURRIEL
INTO info_Emp
FROM Employe e
WHERE idEmploye= code_Emp;
RETURN info_Emp;
EXCEPTION
when no_data_found then
return 'Aucun employe';
END;
/
ACCEPT val NUMBER PROMPT 'Entrer le code Employé: '
DECLARE
code_Emp NUMBER;
BEGIN
code_Emp := '&val';
select fNombreProjets(code_Emp) result from dual;
END;
A simple option, which requires just some minor changes to code you already wrote, is a function that returns ref cursor.一个简单的选项是 function,它只需要对您已经编写的代码进行一些小的更改,它返回 ref cursor。
CREATE OR REPLACE FUNCTION fNombreProjets(code_Emp IN Employe.idEmploye%type)
RETURN sys_refcursor
is
info_Emp sys_refcursor;
BEGIN
OPEN info_Emp for
SELECT e.IDEMPLOYE ,
e.NOMEMPLOYE ,
e.ADRESSE ,
e.TELEPHONE ,
e.ADRESSECOURRIEL
FROM Employe e
WHERE idEmploye = code_Emp;
RETURN info_Emp;
END;
/
Call it then as然后称它为
ACCEPT val NUMBER PROMPT 'Entrer le code Employé: '
SELECT fNombreProjets(&val) result FROM dual;
[EDIT, to answer question you posted as a comment] [编辑,回答您作为评论发布的问题]
Types first (I'll use Scott's emp
sample table for this demo as I don't have your tables):首先输入类型(我将使用 Scott 的
emp
示例表进行此演示,因为我没有您的表):
SQL> CREATE OR REPLACE TYPE t_emp_row AS OBJECT
2 (
3 empno NUMBER,
4 ename VARCHAR2 (10),
5 job VARCHAR2 (10),
6 sal NUMBER
7 );
8 /
Type created.
SQL> CREATE OR REPLACE TYPE t_emp_tab AS TABLE OF t_emp_row;
2 /
Type created.
Function: Function:
SQL> CREATE OR REPLACE FUNCTION fNombreProjets (code_emp IN NUMBER)
2 RETURN t_emp_tab
3 IS
4 retval t_emp_tab;
5 BEGIN
6 SELECT t_emp_row (empno,
7 ename,
8 job,
9 sal)
10 BULK COLLECT INTO retval
11 FROM emp
12 WHERE empno = code_emp;
13
14 RETURN retval;
15 END;
16 /
Function created.
Testing (employee that exists):测试(存在的员工):
SQL> SELECT * FROM TABLE (fNombreProjets (7369));
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
7369 SMITH CLERK 920
Employee that doesn't exist:不存在的员工:
SQL> SELECT * FROM TABLE (fNombreProjets (-7369));
no rows selected
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.