Stored procedures and stored functions refer to the subroutines stored in the database for all user programs to call, called stored procedures and stored functions. The stored procedure has no return value. Stored functions have return values Create stored procedure Create stored procedures and stored functions with the CREATE PROCEDURE command. grammar: create [or replace] PROCEDURE procedure name (parameter list) AS PLSQL subroutine body; Stored procedure example: increase the salary by 10% on the basis of the original salary for the specified employee /* Increase the salary by 10% on the basis of the original salary for the designated employee, and print the salary before and after the salary */ SQL> create or replace procedure raiseSalary(empid in number) as pSal emp.sal%type;--Save the current salary of the employee begin -- Query the employee's salary select sal into pSal from emp where empno=empid; - Give the employee a salary increase update emp set sal = sal*1.1 where empno=empid; --Print the salary before and after the salary increase dbms_output.put_line('Employee number:' || empid || 'Before salary increase ' || psal || 'After salary increase' || psal*1.1); end; 1 / Procedure created --stored procedure call --method one SQL> set serveroutput on SQL> exec raisesalary(7369); Employee number: 7369 Before salary increase 800 880 after salary increase Method Two set serveroutput on begin raisesalary(7369); end; / PL/SQL procedure successfully completed stored function A function is a named stored procedure that can take parameters and return a computed value. Functions and procedures are similar in structure, but must have a RETURN clause that returns the function value. The function specification specifies the function name, the type of the result value, and the parameter type. Syntax to create a stored function: CREATE [OR REPLACE] FUNCTION function name (parameter list) RETURN function value type AS PLSQL subroutine body; Example: Query the annual income of an employee. SQL> /**/ /* Query the total income of an employee */ create or replace function queryEmpSalary(empid in number) return number as pSal number; --Define a variable to hold the employee's salary pComm number; -- define a variable to hold the employee's bonus begin select sal,comm into psal,pcomm from emp where empno = empid; return psal*12+nvl(pcomm,0); end; / Function created l function call SQL> declare v_sal number; begin v_sal:=queryEmpSalary(7934); dbms_output.put_line('salary is:'|| v_sal); end; / salary is:15600 PL/SQL procedure successfully completed SQL> begin dbms_output.put_line('salary is:'|| queryEmpSalary(7934)); end; / salary is:15600 PL/SQL procedure successfully completed trigger A database trigger is a stored PL/SQL program associated with a table. Whenever a specific data manipulation statement (Insert, update, delete) is issued on the specified table, Oracle automatically executes the sequence of statements defined in the trigger. type of trigger Statement level triggers Executes once before or after the specified action statement, regardless of how many rows the statement affects. Row-level triggers (FOR EACH ROW) Every record that the trigger statement acts on is triggered. Use old and new pseudo-record variables in row-level triggers to identify the state of a value. Create a trigger CREATE [or REPLACE] TRIGGER trigger name {BEFORE | AFTER} {DELETE | INSERT | UPDATE [OF列名]} ON table name [FOR EACH ROW [WHEN(条件) ] ] PLSQL block Example 1: Restrict inserting data into the database during non-working hours SQL> create or replace trigger securityEmp before insert on emp declare begin if to_char(sysdate,'day')in('Thursday','Saturday','Sunday') or to_number(to_char(sysdate,'hh24'))not between 8 and 18 then raise_application_error(-20001, 'Cannot insert data during non-working hours.'); end if; end; / Trigger created Trigger Statement and Pseudo Record Variable Value trigger statement :old :new Insert All fields are empty (null) data to be inserted Update Update the previous value of the row updated value delete delete the previous value of the row All fields are empty (null) Example 2: Confirm data (check that the modified value of sal in the emp table is not lower than the original value) SQL> create or replace trigger checkSal before update of sal on emp for each row declare begin if :new.sal<:old.sal then raise_application_error(-20001, 'The salary after the update is smaller than before the update'); end if; end; / Trigger created Result after running: SQL> update emp set sal=260 where empno=7499; update emp set sal=260 where empno=7499 ORA-20001: updated salary is less than before update ORA-06512: 在 "SCOTT.CHECKSAL", line 4 ORA-04088: error during execution of trigger 'SCOTT.CHECKSAL' Trigger Summary Triggers can be used for • Data confirmation • Implement complex security checks • Do auditing, track data operations done on tables, etc. Query triggers, procedures, and functions • Select * from user_triggers; • Select * from user_source;
The use of database stored procedures and stored functions in Oracle
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326120089&siteId=291194637
Recommended
Ranking