Adi*_*dil 5 oracle plsql stored-procedures
编写一个 PL/SQL 过程,将员工编号和薪水作为输入参数,并从经理为 'BLAKE' 且薪水在 1000 到 2000 之间的员工表中删除。
我写了下面的代码:-
create or replace procedure processing(v_emp_no in emp1.empno%type,v_salary in emp1.sal%type)
is
begin
select empno,sal into v_emp_no,v_salary
from emp where ename = 'BLAKE' and sal between 1000 and 2000;
delete from emp1
where empno = v_emp_no
and sal = v_salary;
end;
Run Code Online (Sandbox Code Playgroud)
获得以下错误:-
第 5 行错误:PLS-00403:表达式“V_EMP_NO”不能用作 SELECT/FETCH 语句的 INTO 目标
输入参数不能在函数或过程中赋值,并且您的输入参数不用作输入,因此将它们声明为局部变量并使用它们。
create or replace procedure processing
is
l_emp_no emp1.empno%type;
l_salary emp1.sal%type;
begin
select empno,sal into l_emp_no,l_salary
from emp where ename = 'BLAKE' and sal between 1000 and 2000;
delete from emp1
where empno = l_emp_no
and sal = l_salary;
end;
Run Code Online (Sandbox Code Playgroud)
将该过程称为
begin
processing;
end;
Run Code Online (Sandbox Code Playgroud)
Edit1 :- 如果您需要获取 emp_no 的输出和 emp1 中删除的薪水,请使用 out 参数。
create or replace procedure processing
(v_emp_no OUT emp1.empno%type,v_salary OUT emp1.sal%type)
is
l_emp_no emp1.empno%type;
l_salary emp1.sal%type;
begin
select empno,sal into l_emp_no,l_salary
from emp where ename = 'BLAKE' and sal between 1000 and 2000;
delete from emp1
where empno = l_emp_no
and sal = l_salary;
v_emp_no:=l_emp_no;
v_salary:=l_salary;
end;
Run Code Online (Sandbox Code Playgroud)
称他们为
declare
p_emp_no emp1.empno%type;
p_salary emp1.sal%type;
begin
processing(p_emp_no,p_salary);
dbms_output.put_line('the employee deleted in emp1 is '||p_emp_no);
dbms_output.put_line('the employee salary is '||p_salary);
end;
Run Code Online (Sandbox Code Playgroud)
问题是“v_emp_no”和“v_salary”是您的过程的输入(IN)。
如果您希望您的过程能够修改它(能够将值放入其中),那么您需要通过在过程中声明 IN OUT 来告诉过程。
例如,您有:
v_emp_no in emp1.empno%type
Run Code Online (Sandbox Code Playgroud)
你需要:
v_emp_no in out emp1.empno%type
Run Code Online (Sandbox Code Playgroud)
编辑
你也许更好可以做的是为“select null from emp where ename = 'BLAKE' and sal between 1000 and 2000”设置一个光标。然后看看那个是否返回任何值。如果是这种情况,您可以将布尔值设置为 TRUE。
稍后有 IF fBoolean THEN 删除.....这样你就不必覆盖“v_emp_no”和“v_salary”。
| 归档时间: |
|
| 查看次数: |
6801 次 |
| 最近记录: |