Home Database Oracle Example of oracle stored procedure

Example of oracle stored procedure

May 11, 2023 am 11:13 AM

A stored procedure is a precompiled database program that contains a set of SQL statements and control statements that can be called when needed. This article will introduce the basic knowledge and examples of Oracle database stored procedures.

1. Basics of stored procedures

1.1 Advantages of stored procedures

Stored procedures are an effective method to improve database performance. They improve the efficiency of application interaction with the database because SQL statements are precompiled on the database side, allowing them to complete operations more quickly when called. It also increases data security because stored procedures can perform permission checks before creating and modifying data in the database.

1.2 Creation of stored procedures

You can use Oracle SQL development tools to create stored procedures. Oracle SQL Developer and SQL Plus are commonly used tools.

The following is the basic syntax for creating a stored procedure:

CREATE [OR REPLACE] PROCEDURE procedure_name
([parameter_name IN/OUT datatype [, parameter_name IN/OUT datatype …]])
IS
BEGIN
statement(s);
EXCEPTION
exception_handler;
END;

The parameters are optional, '[OR REPLACE]' command You can specify that the application must exist and retain the state of the stored procedure.

1.3 Input and output parameters of stored procedures

Stored procedures can accept input parameters and output parameters. Input parameters can be used to perform conditional operations within a stored procedure or to pass data to a stored procedure. Output parameters are used to return information such as values ​​or specified values ​​in the output process.

The following is how some parameters interact:

IN: Input parameters are used to pass values ​​to the stored procedure.

OUT: Output parameters are not used for input data, but can return values ​​through stored procedures.

INOUT: Input/output parameters allow a value to be passed as a parameter and changed through the execution return value of the stored procedure.

1.4 Exception handling of stored procedures

Stored procedures can handle exceptions like functions. When an error occurs in the stored procedure, you can set up an exception handling. It can implement the management of custom error messages and use specified behaviors to submit these errors when errors occur.

The following is the basic syntax for creating exception handling:

DECLARE
exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT (exception_name, error_code);
BEGIN
statement(s) ;
EXCEPTION

  WHEN exception_name THEN
     statement(s);
Copy after login

END;

2. Stored procedure examples

The following are some common stored procedure examples:

2.1 Stored procedure Simple query

The following is a simple stored procedure example, which will output data that meets the conditions in the table:

CREATE OR REPLACE PROCEDURE get_emp_data
(
ID IN NUMBER,
NAME OUT VARCHAR2,
SALARY OUT NUMBER
)
IS
BEGIN
SELECT employee_name,salary INTO NAME,SALARY FROM employees WHERE employee_id = ID;
END;

The above stored procedure instance needs to pass in two parameters: ID is a required input parameter, which defines the employee ID for which information is to be queried; while name and salary are output parameters, which accept the values ​​of the corresponding columns in the query results. .

To retrieve the value of the output parameter of the stored procedure, you can call the stored procedure like a function:

DECLARE
emp_name VARCHAR2(20);
emp_salary NUMBER(10,2);
BEGIN
get_emp_data (100,emp_name,emp_salary);
DBMS_OUTPUT.PUT_LINE('Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || emp_salary);
END;

In the above code, the stored procedure parameter ID is set to 100, so the employee's name and salary will be returned.

2.2 Insertion operation of stored procedure

The following is an example of a stored procedure, which implements the function of inserting a row of data into the specified employee roster:

CREATE OR REPLACE PROCEDURE add_employee
(
ID IN NUMBER,
NAME IN VARCHAR2,
AGE IN NUMBER,
SALARY IN NUMBER
)
IS
BEGIN
INSERT INTO employees VALUES ( ID,NAME,AGE,SALARY);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee added.');
EXCEPTION
WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('Error adding employee.');
Copy after login

END;

The above stored procedure example requires 4 input parameters: employee ID, employee name, employee age and employee salary, and then inserts them into the "employees" table. When the insertion is successful, the "employee added" message will be prompted, and when the insertion fails, the "Error adding employee" message will be prompted.

2.3 Update operation of stored procedure

The following example provides the function of increasing the salary of an employee with a specified ID in the employee table by 10%:

CREATE OR REPLACE PROCEDURE increase_employee_salary
(
ID IN NUMBER
)
IS
CURSOR c_employee_salary IS

SELECT salary FROM employees WHERE employee_id = ID;
Copy after login

v_employee_salary NUMBER;
BEGIN
OPEN c_employee_salary;
FETCH c_employee_salary INTO v_employee_salary;
v_employee_salary := v_employee_salary * 1.1;
UPDATE employees SET salary = v_employee_salary WHERE employee_id = ID;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary increased.');
EXCEPTION
WHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE('Employee not found.');
Copy after login

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('Error increasing salary.');
Copy after login

END;

The above stored procedure example requires 1 input parameter: employee ID, which gets the employee's salary based on the employee ID, multiplies it by 1.1 and updates it to the table. When updated correctly, the message "salary increased" will be prompted; when the employee cannot be found, the message "employee not found" will be prompted; when other errors occur, the message "error increasing salary" will be prompted.

Summary

In this article, we introduced the basics of Oracle database stored procedures and some examples. Stored procedures can improve database performance and data security, and are very useful for tasks that need to be performed frequently. Through some examples, you can better understand how to create and use Oracle stored procedures.

The above is the detailed content of Example of oracle stored procedure. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I use cursors in PL/SQL to process multiple rows of data? How do I use cursors in PL/SQL to process multiple rows of data? Mar 13, 2025 pm 01:16 PM

This article explains PL/SQL cursors for row-by-row data processing. It details cursor declaration, opening, fetching, and closing, comparing implicit, explicit, and ref cursors. Techniques for efficient large dataset handling and using FOR loops

What are the commonly used segments in oracle databases What are the commonly used segments in oracle databases Mar 04, 2025 pm 06:08 PM

This article examines Oracle database segment types (data, index, rollback, temporary), their performance implications, and management. It emphasizes choosing appropriate segment types based on workload and data characteristics for optimal efficienc

What are the performance testing tools for oracle databases What are the performance testing tools for oracle databases Mar 04, 2025 pm 06:11 PM

This article explores Oracle database performance testing tools. It discusses selecting the right tool based on budget, complexity, and features like monitoring, diagnostics, workload simulation, and reporting. The article also details effective bo

How to download oracle database How to download oracle database Mar 04, 2025 pm 06:07 PM

This article guides users through downloading Oracle Database. It details the process, emphasizing edition selection (Express, Standard, Enterprise), platform compatibility, and license agreement acceptance. System requirements and edition suitabil

What are the oracle database installation client tools? What are the oracle database installation client tools? Mar 04, 2025 pm 06:09 PM

This article explores Oracle Database client tools, essential for interacting with Oracle databases without a full server installation. It details commonly used tools like SQL*Plus, SQL Developer, Enterprise Manager, and RMAN, highlighting their fun

What default tablespaces does the oracle database provide? What default tablespaces does the oracle database provide? Mar 04, 2025 pm 06:10 PM

This article examines Oracle's default tablespaces (SYSTEM, SYSAUX, USERS), their characteristics, identification methods, and performance implications. It argues against relying on defaults, emphasizing the importance of creating separate tablespac

How do I create users and roles in Oracle? How do I create users and roles in Oracle? Mar 17, 2025 pm 06:41 PM

The article explains how to create users and roles in Oracle using SQL commands, and discusses best practices for managing user permissions, including using roles, following the principle of least privilege, and regular audits.

How do I use Oracle Data Masking and Subsetting to protect sensitive data? How do I use Oracle Data Masking and Subsetting to protect sensitive data? Mar 13, 2025 pm 01:19 PM

This article details Oracle Data Masking and Subsetting (DMS), a solution for protecting sensitive data. It covers identifying sensitive data, defining masking rules (shuffling, substitution, randomization), setting up jobs, monitoring, and deployme

See all articles