Procedures, functions, triggers the creation and use

Creation process

// format
 the Create  or  the replace  Procedure procedure_name (p_name of the type)
 AS 
v_name of the type; 
the begin 
  ***** 
End ;
 // instance with employee number, enter the sector and to find wage increases
 the Create  or  the replace  Procedure add_sal (p_empno emp.empno % of the type)
 AS 
   v_addsal Number ( . 4 ); 
   v_deptno emp.deptno % type;
 the begin 
   SELECT DEPTNO INTO v_deptno from EMP WHERE EMPNO = p_empno;
    IF     v_deptno=10 then v_addsal:=150;
   elsif v_deptno=20 then v_addsal:=200;
   elsif v_deptno=30 then v_addsal:=250;
   else  v_addsal:=300;
   end if;
   update emp set sal = sal + v_addsal where
          empno = p_empno;
   dbms_output.put_line('To increase the ' || v_addsal || ' meta ' );
  End ;
 // call procedure
 Execute add_sal ( 7788 ); 
// or
DECLARE
the begin
  add_sal (7788);
End;

Creating functions

// to employee number as a parameter and returns the average wage in the sector where the employee.
Create  or  Replace  function avg_sal (f_empno in emp.empno % type)
 return emp.sal % type
 AS  
  avg_sal emp.sal % type;
 the begin 
  SELECT  AVG (SAL) INTO avg_sal from EMP WHERE  
  DEPTNO = ( SELECT DEPTNO from EMP WHERE EMPNO = f_empno );
   return avg_sal;
 End ;
 // call
begin
    dbms_output.put_line(avg_sal(7788));
end;

trigger

// When you create a trigger on the emp table, when you insert, delete, or modify employee information,
 // statistics of the number of employees and average wages after the operation, and output.
the Create  or  the replace  the Trigger count_avg_sal 
the After INSERT  or  the Delete  or  Update  
ON emp
 DECLARE 
  v_avg_sal emp.sal % of the type; 
  v_count    Number The ;
 the begin 
  the SELECT  AVG (SAL), COUNT ( * ) INTO v_avg_sal, v_count from emp; 
  dbms_output.put_line ( ' average wage : ' || v_avg_sal ||' The total number of people is: ' || v_count);
 End ; 

// trigger condition is met will trigger
 Update emp the SET SAL = 3000  the WHERE empno = 7788 ;

 

Guess you like

Origin www.cnblogs.com/mobai95/p/12516644.html