繁体   English   中英

如何解决Oracle数据库中缺少或无效的选项?

[英]How to resolve the missing or invalid option in oracle database?

SET SERVEROUTPUT on

declare
       type emp_record is record
       (v_empid emp1.empno % type;
        v_ename emp1.ename % type; )
emp_rec emp_record;
v_sal emp1.sal%type;

begin
v_sal:=:v_sal;

select empno,ename
into emp_rec
from emp1
where sal:=v_sal;

dbms_output.put_line('The employee whose salary is ' || v_sal || 'has Employee Id ' || emp_rec.v_empid || 'and his name is ' || emp_rec.v_ename); 
end;

错误是:

ORA-00922:丢失或无效的选项

代码中有几个问题。 转换为以下内容之一:

declare
   type emp_record is record
   (v_empid emp1.empno % type,
    v_ename emp1.ename % type );

   emp_rec emp_record;
   v_sal emp1.sal%type:=&i_sal;

begin

   select empno,ename
     into emp_rec
     from emp1
    where sal = v_sal;

   dbms_output.put_line('The employee whose salary is ' || v_sal || 'has Employee Id ' 
                        || emp_rec.v_empid || 'and his name is ' || emp_rec.v_ename); 
end;
/

每当提示输入i_sal ,您都将输入所需的薪水参数值。

演示

如果您有多个记录要按@APC所述进行操作,例如有多个人的薪水相同,则需要这样的循环语句:

declare
   type emp_record is record
   (v_empid emp1.empno % type,
    v_ename emp1.ename % type,
    v_sal   emp1.sal % type);

   emp_rec emp_record;
   v_sal emp1.sal%type:=&i_sal;

   cursor c_emp is
   select empno,ename,sal
     into emp_rec
     from emp1
    where sal = v_sal;
begin

  open c_emp;
  loop
    fetch c_emp into emp_rec;
   exit when c_emp%notfound;
    dbms_output.put_line('The employee whose salary is ' || emp_rec.v_sal || ' has Employee Id ' 
                         || emp_rec.v_empid || ' and his name is ' || emp_rec.v_ename); 
  end loop;
  close c_emp;
end;
/

演示

解决该特定问题的最简单方法是使用聚合:

select max(empno), max(ename)
into emp_rec
from emp1
where sal = v_sal;

没有group by的聚合查询总是返回恰好一行。

也就是说,更正确的方法是处理由于没有匹配值而产生的异常。 您还需要处理重复值的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM