写入函数返回每个员工的部门名称。 写一个块打印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;
答案 0 :(得分: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;
答案 1 :(得分: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;