search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Basic Syntax of a PL/SQL Stored Procedure
Example: Creating a Simple Stored Procedure
How to Execute a Stored Procedure
Key Points to Remember
Home Database Oracle How to use PL/SQL to write a stored procedure in an Oracle database

How to use PL/SQL to write a stored procedure in an Oracle database

Jan 01, 2026 am 05:17 AM

To write a PL/SQL stored procedure, you need to use CREATE PROCEDURE to define the structure, including parameters, variable declarations, business logic and exception handling; for example, raise_salary adjusts the salary by entering the employee ID and percentage, and handles exceptions during execution to ensure data consistency.

How to use PL/SQL to write a stored procedure in an Oracle database

To write a stored procedure in PL/SQL for an Oracle database, you need to understand the basic structure of a procedure and how to use the CREATE PROCEDURE statement. A stored procedure is a named block of code that performs one or more tasks and can accept input parameters, return values ​​through output parameters, and be reused across applications.

Basic Syntax of a PL/SQL Stored Procedure

A stored procedure is defined using the following structure:

CREATE [OR REPLACE] PROCEDURE procedure_name ( parameter1 IN datatype, parameter2 OUT datatype, parameter3 IN OUT datatype ) IS -- Declare local variables here BEGIN -- Executable statements go here EXCEPTION -- Exception handling (optional) END;

The IS keyword (or AS ) begins the declaration section. Local variables, cursors, or nested procedures can be declared here. The actual logic goes in the BEGIN...END block.

Example: Creating a Simple Stored Procedure

Suppose you have a table called employees with columns employee_id , name , and salary . You want to create a procedure that increases an employee's salary by a given percentage.

CREATE OR REPLACE PROCEDURE raise_salary ( p_employee_id IN NUMBER, p_percent IN NUMBER ) IS v_current_salary employees.salary%TYPE; BEGIN SELECT salary INTO v_current_salary FROM employees WHERE employee_id = p_employee_id;

UPDATE employees
SET salary = salary * (1 p_percent / 100)
WHERE employee_id = p_employee_id;

COMMIT;

DBMS_OUTPUT.PUT_LINE('Salary updated for employee ID: ' || p_employee_id);

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
ROLLBACK;
END raise_salary;

This procedure takes two input parameters: the employee ID and the percentage increase. It retrieves the current salary, updates it, commits the change, and prints a message. If the employee isn't found, it handles the exception gracefully.

How to Execute a Stored Procedure

You can run the procedure using an anonymous PL/SQL block or from tools like SQL*Plus, SQL Developer, or other Oracle clients.

BEGIN raise_salary(101, 10); -- Increase salary of employee 101 by 10% END;

Alternatively, in SQL*Plus or SQL Developer, you can use:

EXEC raise_salary(101, 10);

Key Points to Remember

  • Use IN parameters to pass values ​​into the procedure.
  • Use OUT parameters if you need to return values ​​to the caller.
  • Use IN OUT if a parameter serves both purposes.
  • Always handle exceptions to avoid unhandled runtime errors.
  • Use COMMIT or ROLLBACK carefully—stored procedures often run as part of larger transactions.
  • Test your procedure with edge cases like invalid IDs or NULL inputs.

Basically, writing a stored procedure in PL/SQL involves defining parameters, declaring variables, writing business logic, and handling errors. Once created, it becomes part of the database schema and can be called from various applications.

The above is the detailed content of How to use PL/SQL to write a stored procedure in an Oracle database. 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

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

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 to use Oracle APEX to build a low-code app? (Rapid Development) How to use Oracle APEX to build a low-code app? (Rapid Development) Mar 13, 2026 am 12:48 AM

OracleAPEXislow-glue,notno-code:itskipsinfrastructurebutrequiresSQL,PL/SQL,anddeclarativelogic;ApplicationProcesseshandleserver-sidevalidationandsideeffects,DynamicActionsmanageclient-sideinteractivity;InteractiveGridneedskey-preservedsourcesforediti

How to manage Flashback Data Archive_Flashback Data Archive table space allocation How to manage Flashback Data Archive_Flashback Data Archive table space allocation Mar 28, 2026 pm 04:06 PM

The reason why the FlashbackDataArchive table space is full is that the hidden history table (SYS_FBA_HIST_XXXXXX) occupies the table space where the main table is located and does not go through ASSM cleaning; you need to use ALTERFLASHBACKARCHIVE...MODIFYTABLESPACE to migrate to the local management automatic segment space table space, and manually clean up the orphan history table.

How to patch Oracle Grid Infrastructure? (System Maintenance) How to patch Oracle Grid Infrastructure? (System Maintenance) Mar 10, 2026 am 01:00 AM

Three things must be confirmed before applying the GI patch: 1. The opatchlsinventory-detail output of each node is consistent; 2. OCR and VoteDisk are online and crsctlcheckcluster-all and ocrcheck both return SUCCESS; 3. $GRID_HOME/crs/install/rootcrs.sh-prepatch has been successfully executed.

How to implement Transparent Data Encryption (TDE) in Oracle? (Data Security) How to implement Transparent Data Encryption (TDE) in Oracle? (Data Security) Mar 13, 2026 am 12:14 AM

OracleTDE must first enable and open the encrypted wallet (Wallet), otherwise ORA-28365 will be reported when executing ALTERTABLESPACE...ENCRYPTION; Wallet needs to be created, opened and managed through the ADMINISTERKEYMANAGEMENT command, and the path must be explicitly configured in sqlnet.ora and permissions must be ensured.

How to troubleshoot the Oracle Listener startup? (Network Services) How to troubleshoot the Oracle Listener startup? (Network Services) Mar 10, 2026 am 12:58 AM

Oraclelistenerstartupfailuresstemfromsilentlistener.oraparsingerrors,hostnameresolutionissues,orpermissionproblems—notbinariesorports;validatesyntaxwithreload,checkownership,verifyactualconfigpath,testDNS,useexplicitIPs,confirmADR_BASE,enabletracingp

How to grant flashback permission_GRANT FLASHBACK ON and FLASHBACK ANY TABLE How to grant flashback permission_GRANT FLASHBACK ON and FLASHBACK ANY TABLE Apr 03, 2026 pm 11:54 PM

FLASHBACK permissions must be explicitly granted: GRANTFLASHBACKONschema.tableTOuser for a single table, and GRANTFLASHBACKANYTABLETOuser for all tables; basic permissions such as SELECT and ALTER and row movement enablement are also required.

How to use JSON data types in Oracle Database? (NoSQL Features) How to use JSON data types in Oracle Database? (NoSQL Features) Mar 08, 2026 am 01:03 AM

In Oracle's JSON scenario, you should select VARCHAR2 (4000CHAR) plus ISJSON constraints (small documents) or BLOB plus ISJSON constraints (large documents), and disable CLOB; ISJSON is a column-level constraint syntax, not a function call; the JSON_VALUE path must be a string literal; JSON_EXISTS needs to be speeded up with the JSON_VALUE function index.

How to grant SYSDBA permissions_sysdba management of password files and OS authentication How to grant SYSDBA permissions_sysdba management of password files and OS authentication Apr 03, 2026 am 08:54 AM

Ordinary users can be authorized through GRANTSYSDBATOusername; provided that the database enables password file authentication (REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE) and has logged in with SYS; there is no need to restart after authorization, but the connection needs to explicitly specify assysdba, and the user credentials must exist in the V$PWFILE_USERS view.

Related articles