Example of oracle stored procedure
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);
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.');
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;
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.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error increasing salary.');
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In addition to SQL*Plus, there are tools for operating Oracle databases: SQL Developer: free tools, interface friendly, and support graphical operations and debugging. Toad: Business tools, feature-rich, excellent in database management and tuning. PL/SQL Developer: Powerful tools for PL/SQL development, code editing and debugging. Dbeaver: Free open source tool, supports multiple databases, and has a simple interface.

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

There are no shortcuts to learning Oracle databases. You need to understand database concepts, master SQL skills, and continuously improve through practice. First of all, we need to understand the storage and management mechanism of the database, master the basic concepts such as tables, rows, and columns, and constraints such as primary keys and foreign keys. Then, through practice, install the Oracle database, start practicing with simple SELECT statements, and gradually master various SQL statements and syntax. After that, you can learn advanced features such as PL/SQL, optimize SQL statements, and design an efficient database architecture to improve database efficiency and security.

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

To view Oracle databases, you can use SQL*Plus (using SELECT commands), SQL Developer (graphy interface), or system view (displaying internal information of the database). The basic steps include connecting to the database, filtering data using SELECT statements, and optimizing queries for performance. Additionally, the system view provides detailed information on the database, which helps monitor and troubleshoot. Through practice and continuous learning, you can deeply explore the mystery of Oracle database.

The procedures, functions and packages in OraclePL/SQL are used to perform operations, return values and organize code, respectively. 1. The process is used to perform operations such as outputting greetings. 2. The function is used to calculate and return a value, such as calculating the sum of two numbers. 3. Packages are used to organize relevant elements and improve the modularity and maintainability of the code, such as packages that manage inventory.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.
