Oracle stored procedures and functions

Source: Internet
Author: User

1. In Oracle, the stored procedure consists of three parts: the definition section, the execution section, and the Exception handling section (that is, the exception)

EG1: Enter the employee number to inquire about the employee's name and salary

Create or Repalce procedure Mypro2 is

Declare--defining parts, defining variables and constants, etc., variable definitions usually begin with v_, and constant definitions usually begin with C_

V_ename varchar2 (20);

V_sal number (7,2);

begin--Executive Section

Select Ename,sal to V_ename,v_sal from EMP where empno=&no;

Dbms_output.put_line (' Employee's name is: ' | | v_ename| | ' Wages are ' | | V_sal);

exception--Exception Handling Section

When No_data_found Then

Dbms_output.put_line (' The employee number you entered does not exist ');

End

EG2: Stored procedure with parameters

--Enter employee's name and new salary, change salary according to name

Create or Replace procedure Mypro2 (pename varchar2,psal number) is

Begin

Update emp set sal=psal where Ename=pename;

End

Methods for calling stored procedures:

A:exec Mypro1 () or call Mypro1 ();

B:exec Mypro1 (' Scott ', 1200) or call Mypro1 (' Scott ', 1200);

2. Functions

function must have a return value of

The structure is as follows: annual salary According to employee's name

Create or Repalce function myfun1 (fename varchar2) return number is yearsal nunber;

Begin

Select SAL*12+NVL (comm,0) to yearsal from EMP where ename=fename;

return yearsal;

End

Methods for calling Functions:

sql> var ys number;
Sql> call Myf1 (' SCOTT ') Into:ys;

Oracle stored procedures and functions

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.