我正在尝试学习 PLSQL,但在创建过程时遇到问题。
我正在解决的任务是:创建一个程序来检查佣金。高于 0.35 的佣金只能为具有 15 年以上经验的员工输入。如果佣金较高或实践较低,则会打印错误。利用异常(Exception 和 Raise)来定义错误消息。
我写了这个,但有一个错误:
<块引用>PLS-00103 在期待以下 ::= 之一时遇到符号 ">"。 (@%;
create or replace PROCEDURE PROVIZIA(num in number) IS
employee_id number;
com_pct employees.commission_pct%type;
begin
select commission_pct into com_pct from employees where employee_id = num;
if PRAX(employee_id) > 15 then
com_pct > 0.35;
else PRAX(employee_id) < 15 then
com_pct < 0.35;
end if;
exception when com_pct > 0.35 and PRAX(employee_id) < 15 then
dbms_output.put_line('error');
raise;
end PROVIZIA;
你能告诉我我哪里出错了吗?
谢谢。
答案 0 :(得分:0)
假设这是一个测试用例(表和 prax
函数返回一些数字;我不知道是哪个以及为什么,所以我做了它以便它为员工 {{1 }}):
1
如果值“错误”会引发错误的过程;不做任何其他事情(因为你没有说在那种情况下该怎么做):
SQL> create table employees as
2 select 1 employee_id, 0.5 commission_pct from dual union all
3 select 2, 0.2 from dual;
Table created.
SQL> create or replace function prax(par_empid in number) return number is
2 begin
3 return case when par_empid = 1 then 10
4 else 50
5 end;
6 end prax;
7 /
Function created.
SQL> select employee_id, commission_pct, prax(employee_id) prax_result
2 from employees;
EMPLOYEE_ID COMMISSION_PCT PRAX_RESULT
----------- -------------- -----------
1 ,5 10 --> this combination is invalid
2 ,2 50 --> this is OK
SQL>
让我们测试一下:
SQL> create or replace procedure provizia(num in number) is
2 com_pct employees.commission_pct%type;
3 l_err exception;
4 begin
5 select commission_pct
6 into com_pct
7 from employees
8 where employee_id = num;
9
10 if com_pct > 0.35 and prax(num) < 15 then
11 raise l_err;
12 end if;
13
14 exception
15 when l_err then
16 raise_application_error(-20000, 'Error');
17 end provizia;
18 /
Procedure created.
SQL>
既然您有一个工作示例,请随时对其进行改进。
答案 1 :(得分:0)
com_ptc > 0.35
你不能这样写。
它返回给你真假。
查看给定的像素。
如果您使用 TOAD.exe,那么您将通知运行时错误。
查看标记区域。
让我们试试这个
CREATE OR REPLACE PROCEDURE PROVIZIA (num IN NUMBER)
IS
employee_id NUMBER;
com_pct employees.commission_pct%TYPE;
BEGIN
SELECT commission_pct
INTO com_pct
FROM employees
WHERE employee_id = num;
IF com_pct > 0.35 AND PRAX (employee_id) < 15
THEN
DBMS_OUTPUT.put_line ('error');
ELSE
DBMS_OUTPUT.put_line ('OK');
END IF;
END PROVIZIA;