首页 > 代码库 > PL/SQL学习(一)

PL/SQL学习(一)

原文参考:http://plsql-tutorial.com/

PL/SQL Block consists of three sections:

  • The Declaration section (optional).
  • The Execution section (mandatory).
  • The Exception Handling (or Error) section (optional).

组成

  • 声明部分(可选)
  • 执行部分(必选)
  • 异常处理(可选)
 
    明:
        DECLARE
 
    执行:
        BEGIN
            ...
        END
 
    异常处理:
        EXCEPTION
 
每个语句必须以“;”结束;
PL/SQL Block可嵌套;
/表示执行PL/SQL块;

These are the Advantages of PL/SQL

    • Block Structures: PL SQL consists of blocks of code, which can be nested within each other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and reused.

    •  Procedural Language Capability: PL SQL consists of procedural language constructs such as conditional statements (if else statements) and loops like (FOR loops).

    •  Better Performance: PL SQL engine processes multiple SQL statements simultaneously as a single block, thereby reducing network traffic.

  • Error Handling: PL/SQL handles errors or exceptions effectively during the execution of a PL/SQL program. Once an exception is caught, specific actions can be taken depending upon the type of the exception or it can be displayed to the user with a message.

优点:
  块结构
         可嵌套,任务或者逻辑模块的集合,可存储在数据库里重用
 
  支持过程化语言
          包含如条件判断语句和循环等过程化语句结构
 
  高性能
          多条SQL语句一起执行,减少了网络通信
 
  异常处理
          异常发生时,可以指定执行特定操作或者返回异常信息
 
PL/SQL变量和常量

变量

格式:

variable_name datatype [NOT NULL := value ]; 
例子:
dept varchar2(10) NOT NULL := “HR Dept”;
 
指定变量值的两种方式:

1)   variable_name:= value;

 2)

       SELECT column_name

       INTO variable_name  

       FROM table_name 

       [WHERE condition];

 
变量范围:
  • Local variables -定义在嵌套块里,不能被外部块访问
  • Global variables - 定义在外部块里,可以供自己使用,也可以被其里面的嵌套块使用。

常量

格式:

constant_name CONSTANT datatype := VALUE; 
例子:
salary_increase CONSTANT number (3) := 10; 
 

PL/SQL学习(一)