PL/SQL programming (I) Basics

Source: Internet
Author: User
Document directory
  • 1. What is PL/SQL?
  • 2. Advantages of PL/SQL
  • 3. Simple PL/SQL Classification
  • 4. PL/SQL programming specifications
  • 5. PL/SQL block (bloc) Introduction
  • 6. instance 1: contains only PL/SQL blocks for execution.
PL/SQL programming


1. Understand PL/SQL concepts

2. Master PL/SQL programming technology, including programming process, functions, triggers, etc.


I. Basic PL/SQL Introduction What is SQL?

PL/SQL (Procedural Language/SQL) is an extension of Oracle in the standard SQL language. PL/SQL not only allows embedding SQL languages, but also allows you to customize variables and constants. conditional statements and cyclic statements can be used to handle various errors with exceptions, this makes its functions more powerful.


2. Advantages of PL/SQL

1) Improve Application Performance

2) modular design concept [paging process, order process, transfer process, etc.]

3) reduce network transmission volume

4) Improve Security

Disadvantages of PL/SQL

1) Poor portability


3. Case studies

1. Create a simple table

Create TableMytest(NameVarchar2 (30),PasswordVarchar (30));

2. insert records to the table

Create Or Replace ProcedureSp_pro1Is


-- Execution part

Insert IntoMytest(Name,Password) Values ('Test','123');


If it is executed in sqlplus, add a slash to the last line/Indicates that the process is completed.


How to call this process?

ExecProcess name(Param list);

CallProcess name(Param list);


3. Simple PL/SQL Classification


4. PL/SQL programming specifications

1) annotations

① Single line comment :--

Select * from EMP where ename = 'Scott '; -- Query employee information

② Multi-line comment

/* This is comments */

2) variable naming rules

① Define the variable. We recommend that you use V _ as the prefix: v_sal

② Define constants. We recommend that you use C _ as the prefix: c_rate

③ Define the cursor. We recommend that you use _ cursor as the Suffix: emp_cursor.

④ Definition exception. We recommend that you use E _ as the prefix: e_error


5. PL/SQL block (bloc) Introduction

Blocks are the basic program units of PL/SQL. Compiling PL/SQL programs is actually compiling PL/SQL blocks. To complete relatively simple application functions, you may only need to write a simple block. However, to implement complex functions, you may need to nest other blocks in a PL/SQL block.

Block Structure

PL/SQL consists of three parts: the definition part, the execution part, and the exception handling part.

As follows:


/* Definition section-Defines variables, constants, cursors, exceptions, complex data types, and so on */


/* Execution part-the PL/SQL statements to be executed and SQL statements */


/* Exception Handling part -- handle various running errors */




1. The definition part starts with declare and is optional;

2. The execution part starts from begin and is required;

3. Exception Handling starts with exception, which is optional.


6. instance 1: contains only PL/SQL blocks for execution.

SetServeroutputOn-- Enable output options


Dbms_output.Putline('Hello world.');


Note: dbms_output is a package provided by Oracle. This package contains some processes. put_line is a built-in process of this package.


Instance 2: PL/SQL blocks that contain the definition part and execution part


V_enameVarchar2 (5);-- Set string variables

V_salNumber (7,2);


SelectEname, SalIntoV_ename, v_salFromEMPWhereEmpno= & No;

-- & Indicates the variable to be received from the console. the pop-up box appears.

Dbms_output.Putline('Employee name :'|V_ename|', Salary :'|V_sal);

-- String Connection Symbol |



Example 3: a PL/SQL block that includes the definition part and execution part and exception handling.

To avoid PL/SQL program running errors and improve PL/SQL robustness, it is necessary to handle possible errors:

① For example, in instance 2, if an employee number does not exist, the exception should be handled.

② Sometimes an error occurs and another logic is required for processing.

Note: Oracle predefines some exceptions. The no_data_found exception means that data cannot be found.


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: 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.