0

编写函数返回每个员工的部门名称。在20个部门写一个块打印所有员工姓名和部门名称。函数头:
Create or replace function empdnm (empno number) return varchar2 is

如果没有这样的empno,请尝试编写异常。

我尝试了以下代码,但它显示了一些错误,即 SQL statment ignored and missing expression. 问题是什么?

create or replace function empdnm (empno1 number) return varchar2 is 
deptname varchar (30);
BEGIN   
Select into deptname(select d.dname from dept d
join emp  e on  e.deptno=d.deptno
Where e.empno= empno1
)
exception
WHEN no_data_found THEN
dbms_output.put_line('no employee with no:'|| empno1);

return(deptname);
end;

BEGIN
FOR r IN (SELECT * FROM emp where dept_id = 20) loop
dbms_output.put_line( 'employee '|| r.emp_name  || ' is in department '||  empdnm(empno1) );
END loop;
end;
4

2 回答 2

2

试试这样:

create or replace function empdnm (empno1 number) return varchar2 is 
   deptname varchar (30);
BEGIN   
   SELECT d.dname
   INTO   deptname
   FROM   dept d
   JOIN   emp e on e.deptno = d.deptno
   WHERE  e.empno = empno1;

   return(deptname);
exception
   WHEN no_data_found THEN
      dbms_output.put_line('no employee with no:'|| empno1);
end;
于 2013-02-05T16:06:47.243 回答
0

使用以下一种:

create or replace function empdnm (empno1 number) return varchar2 is 
   deptname varchar (30);
BEGIN   
   SELECT d.dname
   INTO   deptname
   FROM   dept d
   JOIN   emp e on e.deptno = d.deptno
   WHERE  e.empno = empno1;

   return(deptname);
exception
   WHEN no_data_found THEN
      dbms_output.put_line('no employee with no:'|| empno1);
end;

BEGIN
FOR r IN (SELECT * FROM emp where dept_id = 20) loop
dbms_output.put_line( 'employee '|| r.emp_name  || ' is in department '||  empdnm(r.empno1) );
END loop;
end;
于 2013-02-05T16:47:15.340 回答