pl/sql练习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pl/sql练习相关的知识,希望对你有一定的参考价值。

创建存储过程出错,可以用show errors查看一下
Warning: Procedure created with compilation errors

SQL> show errors;
Errors for PROCEDURE SCOTT.INSERT_DEPT:
LINE/COL ERROR
-------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2/58     PLS-00103: Encountered the symbol "(" when expecting one of the following:       := . ) , @ % default character  The symbol ":=" was substituted for "(" to continue. 
3/61     PLS-00103: Encountered the symbol "(" when expecting one of the following:       := . ) , @ % default character  The symbol ":=" was substituted for "(" to continue. 


查找存储过程
SQL> select object_name,procedure_name,object_type from user_procedures;
drop procedure proc1;


字符型参数不能加长度,也就是v_dname in varchar2可以,但是v_dname in varchar2(10)会报错
create or replace procedure insert_dept(v_deptno in number,
                                        v_dname in varchar2,
                                        v_loc    in varchar2) is
  e_dept_err exception;
  pragma exception_init(e_dept_err, -0001);
begin
  insert into dept values(v_deptno, v_dname, v_loc);
  commit;
exception
  when e_dept_err then
    dbms_output.put_line(‘You deptno is not unique,please input unique deptno number!‘);
  when others then
    dbms_output.put_line(‘other error‘);

end;


SQL> exec insert_dept(80,‘zhangsan‘,‘beijing‘)
PL/SQL procedure successfully completed
SQL> exec insert_dept(80,‘zhangsan‘,‘beijing‘)
You deptno is not unique,please input unique deptno number!
PL/SQL procedure successfully completed



存储过程是带有名称的pl/sql块
包含两种:带参数与不带参数的存储过程
下午:

带有out参数的存储过程
create or replace procedure proc2
(v_empno number,v_ename out varchar2,v_sal out number)
is
begin
  select ename,sal into v_ename,v_sal from emp where empno=v_empno;
  dbms_output.put_line(‘Employee name is : ‘||v_ename||‘ Employee salary is: ‘||v_sal);
exception
  when no_data_found then
    dbms_output.put_line(‘Employee not exist,please input currect number‘);
  when others then
    dbms_output.put_line(‘other errors‘);
end;

定义两个参数接收块里的输出参数值
SQL> var v_name varchar2(10);
SQL> var v_salary number;
SQL> exec proc2(7369,:v_name,:v_salary);  ----调用系统参数时,前边加:
Employee name is : SMITH Employee salary is: 2600
PL/SQL procedure successfully completed
v_name
---------
SMITH
v_salary
---------
2600

SQL> print v_name
v_name
---------
SMITH

SQL> print v_salary
v_salary
---------
2600


或者用匿名块的方式来执行proc2
declare
v_name emp.ename%type;
v_salary emp.sal%type;
begin
proc2(7369,:v_name,:v_salary);
end;



带有输入和输出型参数的存储过程
create or replace procedure proc3(v_empno in out number,
                                  v_ename out varchar2,
                                  v_sal   out number) is
begin
  select empno, ename, sal
    into v_empno, v_ename, v_sal
    from emp
   where empno = v_empno;
  dbms_output.put_line(v_empno);
  dbms_output.put_line(v_ename);
  dbms_output.put_line(v_sal);
exception
  when no_data_found then
    dbms_output.put_line(‘Employee not exist,please input currect number‘);
  when others then
    dbms_output.put_line(‘other errors‘);
end;



declare
v_empno emp.empno%type;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
  v_empno:=7369;
  proc3(v_empno,v_ename,v_sal);
end;



函数
函数用于返回特定数据
函数作为表达式的一部分被调用
函数可以简化客户端应用程序的开发
提高应用程序的执行性能。


不带有任何参数的函数
create or replace function fun1
return number
is
v_sum_sal emp.sal%type;
begin
select sum(sal) into v_sum_sal  from emp where deptno=10;
return v_sum_sal;
end;

declare
v_sumsal emp.sal%type;
begin
  v_sumsal := fun1;
  dbms_output.put_line(v_sumsal);
end;


升级:
create or replace function fun2
(v_deptno emp.deptno%type)
return number
is
v_sum_sal emp.sal%type;
begin
select sum(sal) into v_sum_sal  from emp where deptno=v_deptno;
return v_sum_sal;
end;

declare
v_sumsal emp.sal%type;
begin
  v_sumsal := fun2(10);
  dbms_output.put_line(v_sumsal);
end;


查询函数情况
select name,text from user_source;


SQL> create or replace function fun2
  2 
  3   (v_empno number , v_ename out varchar2,v_sal out number)
  4 
  5   return number
  6 
  7   is
  8 
  9   begin
 10 
 11     select ename,sal into v_ename,v_sal from emp where empno=v_empno;
 12     return v_sal;
 13   end;
 14  /
Function created

SQL> declare
  2 
  3     v_salary emp.sal%type;
  4     v_ename emp.ename%type;
  5     v_sal emp.sal%type;
  6 
  7   begin
  8       v_salary := fun2(7369,v_ename,v_sal);
  9       dbms_output.put_line (‘Salary is: ‘||v_salary);
 10 
 11   end;
 12  /
Salary is: 6800
PL/SQL procedure successfully completed


create or replace function  fun_sum_sal
(v_empno in number)
return number
is
v_sumsal emp.sal%type;
begin
  select (sal+nvl(comm,0))*12 into v_sumsal from emp where empno=v_empno;
  return v_sumsal;
end;


declare
v_sumsalary number;
begin
  v_sumsalary := fun_sum_sal(7369);
  dbms_output.put_line(v_sumsalary);
end;



以上是关于pl/sql练习的主要内容,如果未能解决你的问题,请参考以下文章

pl/sql练习

PL/SQL练习函数

PL/SQL 触发器练习

pl/sql练习案例

PL/SQL练习自定义异常

练习2 及pl/sql