📅  最后修改于: 2020-11-26 05:48:07             🧑  作者: Mango
在本章中,我们将讨论PL / SQL的基本语法,它是一种块结构语言。这意味着将PL / SQL程序划分并写入逻辑代码块中。每个块包含三个子部分-
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语句均以分号(;)结尾。可以使用BEGIN和END将PL / SQL块嵌套在其他PL / SQL块中。以下是PL / SQL块的基本结构-
DECLARE
BEGIN
EXCEPTION
END;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
结束;行表示PL / SQL块结束。要从SQL命令行运行代码,您可能需要在代码的最后一行之后的第一个空白行的开头键入/。当以上代码在SQL提示符下执行时,将产生以下结果-
Hello World
PL/SQL procedure successfully completed.
PL / SQL标识符是常量,变量,异常,过程,游标和保留字。标识符由一个字母组成,可以选择在其后跟随更多字母,数字,美元符号,下划线和数字符号,并且不得超过30个字符。
默认情况下,标识符不区分大小写。因此,您可以使用整数或整数表示数字值。您不能使用保留关键字作为标识符。
分隔符是具有特殊含义的符号。以下是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 |
程序注释是解释性语句,可以包含在您编写的PL / SQL代码中,可帮助任何人阅读其源代码。所有编程语言都允许某种形式的注释。
PL / SQL支持单行和多行注释。 PL / SQL编译器将忽略任何注释中可用的所有字符。 PL / SQL单行注释以定界符-(双连字符)开头,多行注释由/ *和* /括起来。
DECLARE
-- variable declaration
message varchar2(20):= 'Hello, World!';
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line(message);
END;
/
当以上代码在SQL提示符下执行时,将产生以下结果-
Hello World
PL/SQL procedure successfully completed.
PL / SQL单元是以下任意一项-
在以下各章中将讨论这些单元中的每个单元。