1--PL/SQL-Main Features of PL/SQL

Main Features of PL/SQL

  • Error Handling
    PL/SQL makes it easy to detect and handle errors.

When an error occurs, PL/SQL raises an exception. Normal execution stops and control transfers to the exception-handling part of the PL/SQL block. You do not have to check every operation to ensure that it succeeded, as in a C program.

  • Blocks

The basic unit of a PL/SQL source program is the block, which groups related declarations and statements.

A PL/SQL block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END. These keywords divide the block into a declarative part, an executable part, and an exception-handling part. Only the executable part is required. A block can have a label.

Declarations are local to the block and cease to exist when the block completes execution, helping to avoid cluttered namespaces for variables and subprograms.

Blocks can be nested: Because a block is an executable statement, it can appear in another block wherever an executable statement is allowed.

You can submit a block to an interactive tool (such as SQL*Plus or Enterprise Manager) or embed it in an Oracle Precompiler or OCI program. The interactive tool or program runs the block one time. The block is not stored in the database, and for that reason, it is called an anonymous block (even if it has a label).

An anonymous block is compiled each time it is loaded into memory, and its compilation has three stages:

Syntax checking: PL/SQL syntax is checked, and a parse tree is generated.

Semantic checking: Type checking and further processing on the parse tree.

Code generation

Note:An anonymous block is a SQL statement.

This example shows the basic structure of a PL/SQL block.

Copy

sql 复制代码
<< label >> (optional)
DECLARE    -- Declarative part (optional)
  -- Declarations of local types, variables, & subprograms

BEGIN      -- Executable part (required)
  -- Statements (which can use items declared in declarative part)

[EXCEPTION -- Exception-handling part (optional)
  -- Exception handlers for exceptions (errors) raised in executable part]
END;
  • Variables and Constants

L/SQL lets you declare variables and constants, and then use them wherever you can use an expression.

As the program runs, the values of variables can change, but the values of constants cannot.

  • Subprograms
    A PL/SQL subprogram is a named PL/SQL block that can be invoked repeatedly.
  • Packages
    A package is a schema object that groups logically related PL/SQL types, variables, constants, subprograms, cursors, and exceptions.

A package is compiled and stored in the database, where many applications can share its contents. You can think of a package as an application.

  • Triggers

    A trigger is a named PL/SQL unit that is stored in the database and run in response to an event that occurs in the database.

  • Input and Output

    Most PL/SQL input and output (I/O) is done with SQL statements that store data in database tables or query those tables. All other PL/SQL I/O is done with PL/SQL packages that Oracle Database supplies.

  • Data Abstraction

    Data abstraction lets you work with the essential properties of data without being too involved with details.

You can design a data structure first, and then design algorithms that manipulate it.

复制代码
- Cursors
- Composite Variables
- Using the %ROWTYPE Attribute
- Using the %TYPE Attribute
- Abstract Data Types
  • Control Statements

Control statements are the most important PL/SQL extension to SQL.

PL/SQL has three categories of control statements:

--Conditional selection statements, which let you run different statements for different data values.

--Loop statements, which let you repeat the same statements with a series of different data values.

--Sequential control statements, which allow you to go to a specified, labeled statement, or to do nothing.

  • Conditional Compilation
    Conditional compilation lets you customize the functionality in a PL/SQL application without removing source text.
  • Processing a Query Result Set One Row at a Time
    PL/SQL lets you issue a SQL query and process the rows of the result set one at a time.

You can use a basic loop, or you can control the process precisely by using individual statements to run the query, retrieve the results, and finish processing.

This example uses a basic loop.

sql 复制代码
BEGIN
  FOR someone IN (
    SELECT * FROM "RESOURCES"."EMPLOYEE"
    WHERE employeeID < 120
    ORDER BY employeeID
  )
  LOOP
    DBMS_OUTPUT.PUT_LINE('TITLE = ' || someone.TITLE ||
                         ', DATE = ' || someone.HAIRDATE);
  END LOOP;
END;
/

Result:

相关推荐
开开心心就好13 分钟前
高效报价软件,简化商铺定价流程
服务器·数据库·安全·面试·职场和发展·电脑·symfony
钢铁男儿1 小时前
PyQt5高级界而控件(容器:装载更多的控件QDockWidget)
数据库·python·qt
阿蒙Amon4 小时前
C# Linq to SQL:数据库编程的解决方案
数据库·c#·linq
互联网搬砖老肖8 小时前
运维打铁: MongoDB 数据库集群搭建与管理
运维·数据库·mongodb
典学长编程9 小时前
数据库Oracle从入门到精通!第四天(并发、锁、视图)
数据库·oracle
积跬步,慕至千里9 小时前
clickhouse数据库表和doris数据库表迁移starrocks数据库时建表注意事项总结
数据库·clickhouse
极限实验室10 小时前
搭建持久化的 INFINI Console 与 Easysearch 容器环境
数据库
星辰离彬10 小时前
Java 与 MySQL 性能优化:Java应用中MySQL慢SQL诊断与优化实战
java·后端·sql·mysql·性能优化
白仑色10 小时前
Oracle PL/SQL 编程基础详解(从块结构到游标操作)
数据库·oracle·数据库开发·存储过程·plsql编程
程序猿小D12 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的个人财务管理系统,推荐!
java·数据库·mysql·spring·毕业论文·ssm框架·个人财务管理系统