Oracle Stored Procedure

1. Definition
The so-called Stored Procedure is a set of SQL statements used to complete specific database functions. The SQL statement set is
compiled and stored in the database system.
When in use, the user calls and executes the stored procedure by specifying the name of the defined stored procedure and giving the corresponding stored procedure parameters , thereby completing one or a series of database operations.

2, the creation of the
stored procedure Oracle stored procedure includes three parts: the procedure declaration, the execution procedure part, the stored procedure exception.

(1) No-parameter stored procedure syntax

copy code

create or replace procedure NoParPro  
 as //declaration  
 ;  
 begin // execute  
 ;  
 exception//Stored procedure exception  
 ;  
 end;

copy code

(2) An example of a stored procedure with parameters

copy code

create or replace procedure queryempname(sfindno emp.empno%type)   
as  
   sName emp.ename%type;  
   sjob emp.job%type;  
begin  
       ....  
exception  
       ....  
end;

copy code

 

(3) Stored procedures with parameters include assignment methods

copy code

create or replace procedure runbyparmeters    
    (isal in emp.sal%type,   
     sname out varchar,  
     sjob in out varchar)  
 as   
    icount number;  
 begin  
      select count(*) into icount from emp where sal>isal and job=sjob;  
      if icount=1 then  
        ....  
      else  
       ....  
     end if;  
exception  
     when too_many_rows then  
     DBMS_OUTPUT.PUT_LINE('The return value is more than 1 line');  
     when others then  
     DBMS_OUTPUT.PUT_LINE('Error during RUNBYPARMETERS!');  
end;

copy code

The parameter IN represents the input parameter and is the default mode of the parameter.
OUT represents the return value parameter, and the type can use any legal type in Oracle.
The parameters defined in the OUT mode can only be assigned values ​​within the procedure body, which means that the parameter can pass a value back to the procedure that called it.
IN OUT means that the parameter can pass a value to the procedure or pass a value out.

(4) Cursor definition use in stored procedure

copy code

as //definition (cursor a result set that can be traversed)   
CURSOR cur_1 IS   
  SELECT area_code,CMCODE,SUM(rmb_amt)/10000 rmb_amt_sn,  
         SUM(usd_amt)/10000 usd_amt_sn   
  FROM BGD_AREA_CM_M_BASE_T   
  WHERE ym >= vs_ym_sn_beg   
       AND ym <= vs_ym_sn_end   
  GROUP BY area_code,CMCODE;   
      
begin //execute (usually For statement traverses the cursor)       
FOR rec IN cur_1 LOOP   
  UPDATE xxxxxxxxxxx_T   
   SET rmb_amt_sn = rec.rmb_amt_sn,usd_amt_sn = rec.usd_amt_sn   
   WHERE area_code = rec.area_code   
   AND CMCODE = rec.CMCODE   
   AND ym = is_ym;   
END LOOP;

copy code

 

(5) Definition of cursor

copy code

--Display cursor processing
declare  
--- declare cursor, create and name a sql workspace
cursor cursor_name is  
    select real_name from account_hcz;
    v_realname varchar2 (20);
begin
    open cursor_name;---Open cursor and execute the result set generated by sql statement
    fetch cursor_name into v_realname;--Extract the cursor, extract the records in the result set
    dbms_output.put_line(v_realname);
    close cursor_name;--close cursor
end;

copy code

3. Calls to stored procedures in Oracle 

(1) Procedure call method 1

copy code

declare  
      realsal emp.sal%type;  
      realname varchar(40);  
      realjob varchar(40);  
begin //procedure call begins  
      realsal:=1100;  
      realname:='';  
      realjob:='CLERK';  
      runbyparmeters(realsal, realname, realjob); - must be in order  
      DBMS_OUTPUT.PUT_LINE(REALNAME||'   '||REALJOB);  
END; //end of procedure call

copy code

(2) Procedure call method 2

copy code

declare  
     realsal emp.sal%type;  
     realname varchar(40);  
     realjob varchar(40);  
begin //procedure call begins  
     realsal:=1100;  
     realname:='';  
     realjob:='CLERK';  
     --The order of the variables corresponding to the specified value can be changed  
     runbyparmeters(sname=>realname,isal=>realsal,sjob=>realjob);           
    DBMS_OUTPUT.PUT_LINE(REALNAME||'   '||REALJOB);  
END; //end of procedure call

copy code

(3) Procedure call mode 3 (under SQL command line mode)

1. SQL>exec proc_emp('parameter 1','parameter 2');//No return value procedure call  
2、SQL>var vsal number  
     SQL> exec proc_emp ('parameter 1',:vsal);// procedure call with return value  
      Or: call proc_emp ('parameter 1',:vsal);// procedure call with return value

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020069&siteId=291194637
Recommended