Using Oracle stored procedures

First, what is stored procedure:
      Stored procedure (Stored Procedure) is a set of functions in order to complete a particular set of SQL statements in the database compiled after storage. Name specified by the user and gives the parameters stored procedure to execute it (if the stored procedure with parameters). A stored procedure is an important object in the database, any well-designed database applications should use stored procedures.
      Stored procedures are written by SQL statements and flow control process that after compiling and optimizing storage in the database server, the application can use just call. In ORACLE, a plurality of associated processes may be combined together to form a package. The following advantages:
   1. stored procedures and functions to name database objects which are stored in the database. Advantages stored in the database is obvious, because the code is not stored locally, users can log on to any database on the client, and call or modify the code.
  2. Stored procedures and functions can be provided by database security guarantees, in order to use stored procedures and functions, need to have permission from the owner of the stored procedures and functions, the user or creator only authorized itself to execute a stored procedure or function call.
  3. The information stored procedures and functions are written in the data dictionary, so the stored procedure can be seen as a common module, user-written PL / SQL stored procedures or other program can call it (but can not call stored procedures and functions PL / SQL program). A reusable function can be designed as a stored procedure.
  4. Procedures and Functions like other high-level language like, you can pass parameters to pass stored procedure or function, the parameters also have a variety of ways. Stored procedures can have return values, you can not have a return value, the return value of the stored procedure parameter must be brought back; function has a certain data type, like other standard functions, we can return by calling the function value of the function name.
  5. Stored procedures and functions need to be compiled in order to rule out syntax errors, it can be called only by the compiler.
 
Second, create a stored procedure:
create [or replace] procedure stored procedure name [(input and output parameters, in / out parameters)]  
is/as  
begin  
  sentences;  
    [exception  
     sentences;]  
end the stored procedure name;  
 
[Example 1 ] stored procedure that takes no parameters:
SQL>set serveroutput on
SQL> create or replace procedure pro_no_par is
  2  begin
  3   update emp set sal=sal+800 where id=3;
  4  commit;
  5 dbms_output.put_line ( 'salary has been adjusted !!');
  6 pro_no_par end;
  7  /
 
SQL> execute pro_no_par;
    Wages have been adjusted !!
PL / SQL procedure successfully completed.
 
 [Example 2 ] stored in the process model with parameters ( mode, there are three parameters: Name passed; passed by position; mixed mode delivery )
 
SQL>  set serveroutput on
SQL>  create or replace procedure pro_in_par
  2   (var_1 in nvarchar2,
  3    var_2 in number) is
  4   begin
  5    update emp set sal=sal+var_2 where ename=var_1;
  6    commit;
  7 dbms_output.put_line (var_1 || 'wages have increased' || var_2 || 'dollars!');
  8  end pro_in_par;
          9  /
       
      SQL> execute pro_in_par('Smith',1500);
     Smith's salary has increased by 1,500 yuan!
 
 
  【例3一个带out模式和in out模式参数存储过程
          
SQL> create or replace procedure pro_out_par
  2  (var_1 in out number,
  3   var_2 out emp.ename%type,
  4   var_3 out emp.sal%type) is
  5  begin
  6   select ename,sal into var_2,var_3 from emp where id=var_1;
  7  end pro_out_par;
  8  /
 
SQL>set serveroutput on
SQL>declare
  2 ex_var_1 number;
  3 ex_var_2 emp.ename%type;
  4 ex_var_3 emp.sal%type;
  5 begin
  6   ex_var_1:=8;
  7   pro_out_par(ex_var_1,ex_var_2,ex_var_3);
  8   dbms_output.put_line('员工号为'||ex_var_1||'的员工姓名是:'||ex_var_2||'工资是:'||ex_var_3);
  9 end;
 10  /
 
    员工号为8的员工姓名是:周瑜工资是:8500
 
三、存储过程参数说明:三种形式的参数
         1. IN 定义一个输入参数变量,用于传递参数给存储过程
       2. OUT 定义一个输出参数变量,用于从存储过程获取数据
       3. IN OUT 定义一个输入、输出参数变量,兼有以上两者的功能
 
四、 删除存储过程
 
              DROP PROCEDURE 存储过程名;
      例如: 
             SQL> drop procedure pro_no_par;
             过程已删除。
 
 

Guess you like

Origin www.cnblogs.com/lone5wolf/p/11520523.html