Example of stored procedures and stored functions and triggers

Source: Internet
Author: User

Example of stored procedures and stored functions and triggers 1. Example of stored procedures: SQL> create or replace procedure raiseSalary (empid in number) as pSal emp. sal % type; begin select sal into pSal from emp where empno = empid; update emp set sal = sal * 1.1 where empno = empid; dbms_output.put_line ('employee ID: '| empid | 'salary raise pre' | psal | 'salary raise Post' | psal * 1.1); end; /Procedure created SQL> set serveroutput on SQL> exec raisesalary (7369); employee No.: 7369 800 PL/SQL procedure successfully completed 2. storage function example: query the annual income of a worker. SQL>/** // * query the total income of a certain employee */create or replace function queryEmpSalary (empid in number) return number as pSal number; -- Define the variable to save the employee's salary pComm number; -- Define the variable to save 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 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 3. create trigger Example 1: restrict non-working hours to insert data to the database SQL> create or replace trigger securityEmp before insert on emp declare begin if to_char (sysdate, 'day') in ('thurday', 'saturday', 'sunday') or to_number (to_char (sysdate, 'hh24') not between 8 and 18 then raise _ Application_error (-20001, 'data cannot be inserted during non-work hours. '); End if; end;/Trigger created 4. Create Trigger Example 2: confirm the data (check that the modified sal value 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, 'salary after the Update'); end if; end;/Trigger created

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.