Plsql 简明教程

PL/SQL - Basic Syntax

在本章中,我们将讨论 PL/SQL 的基本语法,这是一种 block-structured 语言;这意味着 PL/SQL 程序被划分为逻辑代码块并编写在这些逻辑代码块中。每个块包含三个子部分:−

In this chapter, we will discuss the Basic Syntax of PL/SQL which is a block-structured language; this means that the PL/SQL programs are divided and written in logical blocks of code. Each block consists of three sub-parts −

S.No

Sections & Description

1

Declarations This section starts with the keyword DECLARE. It is an optional section and defines all variables, cursors, subprograms, and other elements to be used in the program.

2

Executable Commands This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It consists of the executable PL/SQL statements of the program. It should have at least one executable line of code, which may be just a NULL command to indicate that nothing should be executed.

3

Exception Handling This section starts with the keyword EXCEPTION. This optional section contains exception(s) that handle errors in the program.

每个 PL/SQL 语句都以分号 (;) 结尾。PL/SQL 块可以使用 BEGINEND 嵌套在其他 PL/SQL 块内。以下是 PL/SQL 块的基本结构:−

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END. Following is the basic structure of a PL/SQL block −

DECLARE
   <declarations section>
BEGIN
   <executable command(s)>
EXCEPTION
   <exception handling>
END;

The 'Hello World' Example

DECLARE
   message  varchar2(20):= 'Hello, World!';
BEGIN
   dbms_output.put_line(message);
END;
/

end; 行表示 PL/SQL 块的结束。要在 SQL 命令行中运行代码,您可能需要在代码最后一行之后的第一个空白行的开头键入 /。在 SQL 提示符处执行以上代码时,它产生以下结果:−

The end; line signals the end of the PL/SQL block. To run the code from the SQL command line, you may need to type / at the beginning of the first blank line after the last line of the code. When the above code is executed at the SQL prompt, it produces the following result −

Hello World

PL/SQL procedure successfully completed.

The PL/SQL Identifiers

PL/SQL 标识符是常量、变量、异常、过程、游标和保留字。标识符由一个字母(后面可以跟更多字母、数字、美元符号、下划线和数字符号)组成,并且不应超过 30 个字符。

PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs and should not exceed 30 characters.

默认情况下, identifiers are not case-sensitive 。因此,你可以使用 integer 或者 INTEGER 来表示数值。你不能将保留关键字用作标识符。

By default, identifiers are not case-sensitive. So you can use integer or INTEGER to represent a numeric value. You cannot use a reserved keyword as an identifier.

The PL/SQL Delimiters

分隔符是具有特殊含义的符号。以下是 PL/SQL 中的分隔符列表:

A delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL −

Delimiter

Description

+, -, *, /

Addition, subtraction/negation, multiplication, division

%

Attribute indicator

'

Character string delimiter

.

Component selector

(,)

Expression or list delimiter

:

Host variable indicator

,

Item separator

"

Quoted identifier delimiter

=

Relational operator

@

Remote access indicator

;

Statement terminator

:=

Assignment operator

Association operator

*

*

Concatenation operator

**

Exponentiation operator

<<, >>

Label delimiter (begin and end)

/, /

Multi-line comment delimiter (begin and end)

--

Single-line comment indicator

..

Range operator

<, >, ⇐, >=

Relational operators

<>, '=, ~=, ^=

Different versions of NOT EQUAL

The PL/SQL Comments

程序注释是可以包括在你所编写的 PL/SQL 代码中的解释性语句,并且有助于其他人阅读其源代码。所有编程语言都允许某种形式的注释。

Program comments are explanatory statements that can be included in the PL/SQL code that you write and helps anyone reading its source code. All programming languages allow some form of comments.

PL/SQL 支持单行和多行注释。PL/SQL 编译器会忽略出现在任何注释中的所有字符。PL/SQL 单行注释以分隔符 (双连字符) 开始,多行注释则由 /* 和 */ 括住。

The PL/SQL supports single-line and multi-line comments. All characters available inside any comment are ignored by the PL/SQL compiler. The PL/SQL single-line comments start with the delimiter — (double hyphen) and multi-line comments are enclosed by /* and */.

DECLARE
   -- variable declaration
   message  varchar2(20):= 'Hello, World!';
BEGIN
   /*
   *  PL/SQL executable statement(s)
   */
   dbms_output.put_line(message);
END;
/

当以上代码在 SQL 提示符下执行时,它会生成以下结果:

When the above code is executed at the SQL prompt, it produces the following result −

Hello World

PL/SQL procedure successfully completed.

PL/SQL Program Units

PL/SQL 单元是以下任一项:

A PL/SQL unit is any one of the following −

  1. PL/SQL block

  2. Function

  3. Package

  4. Package body

  5. Procedure

  6. Trigger

  7. Type

  8. Type body

下文章节将讨论这些单元中的每个单元。

Each of these units will be discussed in the following chapters.