create or replace procedure d_name_legzdins (id in dept.deptno%type) as d_name_legzdins varchar(20);
BEGIN
SELECT dname into d_count_legzdins FROM dept WHERE deptno=id;
if (SELECT count(empno) from emp where deptno=id)=0 THEN
raise_application_error(-20101, 'There are no employees currently working at this department!');
else
DBMS_OUTPUT.PUT_LINE('There are currently '|| (SELECT count(empno) from emp where deptno=id) ||' employees working at '|| d_count_legzdins ||'department');
END IF;
exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE('There is no such department!');
end;'
我刚刚在大学开始这个主题,无论我做什么都无法让它发挥作用
供参考
表dept包含deptno,即derpartment的id和dname,即名称
并且emp包含empno,whcih是员工姓名,deptno是同一个dept id并且是connected
答案 0 :(得分:1)
Emils,您的代码中有一些严重的语法错误。可以在 Oracle Docs 中查看 PL/SQL 语法和语义的快速参考指南。 您的代码可以这样更改:
create or replace procedure d_name_legzdins (id in dept.deptno%type)
as
v_name_legzdins varchar(20);
v_emp_cnt number;
begin
select dname
into v_name_legzdins
from dept
where deptno=id;
begin
select count(empno)
into v_emp_cnt
from emp where
deptno=id;
exception
when no_data_found then
raise_application_error(-20101, 'There are no employees currently working at this department!');
end;
DBMS_OUTPUT.PUT_LINE('There are currently '|| v_emp_cnt ||' employees working at '|| v_name_legzdins ||' department');
exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE('There is no such department!');
end;
答案 1 :(得分:0)
正如所指出的,您有一些严重的语法错误。清洁这些显然是您的首要任务。但是恕我直言,您的程序布局有些不稳定。您的结构为“良好流程,然后是错误流程,然后是良好流程,然后是错误流程”。我发现一个更清晰的过程是“好的过程然后所有的异常处理”(不可否认,有时是不可能的)。在这种情况下,部门中缺少 Dept 和 No Emp 都是例外。所以在 EXCEPTION 部分处理两者。 Oracle 允许嵌套块,并且每个块都可以有一个异常部分:例如(参见 fiddle]1)。
create or replace procedure d_name_legzdins (id in dept.deptno%type)
as
v_name_legzdins varchar(20);
v_emp_cnt number;
begin
select d.dname, count(*)
into v_name_legzdins,v_emp_cnt
from dept d
join emp e on (e.deptno = d.deptno)
where d.deptno = id
group by d.dname;
dbms_output.put_line('There are currently '|| v_emp_cnt ||
' employees working at '|| v_name_legzdins ||' department'
);
exception
when no_data_found then
-- at this point we know that either the parameter for department (id) is invalid
-- or that it is valid but has no employees. Now discover which.
begin
select dname
into v_name_legzdins
from dept
where deptno = id
and rownum <= 1;
-- the department exists, so it does not have any employees.
raise_application_error(-20101, 'There are no employees currently working at this department!');
exception
when no_data_found then
dbms_output.put_line('There is no such department!');
end ; -- inner exception
end d_name_legzdins;
我不明白为什么你只想要一条关于缺少仪态的消息,而想要一个用户定义的没有员工的例外,但这种不一致是你的选择。