首页 > 数据库 > Oracle > PLSQL简单的程序之二
这两个例子里面,程序执行的任务是一样的。这里其中这两个例子都
使用了“&employee_id”,第二个还使用了%type数据类型(与源表的数据类型保持一致)
---编写程序块用来查询员工号为205员工的工资,并考虑员工号不存在的情况。
hr@PROD>declare
2 v_esalary number(8,2);
3 begin
4 select salary into v_esalary
5 from employees where employee_id=&employee_id;
6 dbms_output.put_line('Their salary are: '||v_esalary);
7 exception
8 when no_data_found then
9 dbms_output.put_line('There is no employee_id found!');
10 end;
11 /
Enter value for employee_id: 205
old 5: from employees where employee_id=&employee_id;
new 5: from employees where employee_id=205;
Their salary are: 12008
PL/SQL procedure successfully completed.
#程序执行完成,并返回一条数据。
---使用赋值变量,输入一个不存在的员工号,再执行一次上述的程序:
hr@PROD>declare
2 v_esalary employees.employee_id%type;
3 begin
4 select salary into v_esalary
5 from employees where employee_id=&employee_id;
6 dbms_output.put_line('Their salary are: '||v_esalary);
7 exception
8 when no_data_found then
9 dbms_output.put_line('There is no employee_id found!');
10 end;
11 /
Enter value for employee_id: 888
old 5: from employees where employee_id=&employee_id;
new 5: from employees where employee_id=888;
There is no employee_id found!
PL/SQL procedure successfully completed.
#该程序执行完成。使用exception来解决不存在数据的情况,来保证程序正常执行。
若不使用exception来抛出程序中程序执行过程中遇到的异常,则会导致整个程序不能执行。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31392094/viewspace-2128347/ ,如需转载,请注明出处,否则将追究法律责任。