In this section, we will talk about the Basic Syntax of PL/SQL which is a square organized language; this implies that the PL/SQL programs are isolated and written in consistent squares of code. Each square comprises 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. |
Each PL/SQL explanation closes with a semicolon (;). PL/SQL squares can be settled inside other PL/SQL blocks utilizing BEGIN and END. Following is the essential construction 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;
/
The end; line flags the finish of the PL/SQL block. To run the code from the SQL order line, you may have to type/toward the start of the principal clear line after the last line of the code. At the point when the above code is executed at the SQL brief, it creates the accompanying outcome −
Hello World
PL/SQL procedure successfully completed.
The PL/SQL Identifiers
PL/SQL identifiers are constants, factors, special cases, methodology, cursors, and held words. The identifiers comprise of a letter alternatively followed by more letters, numerals, dollar signs, underscores, and number signs and ought not surpass 30 characters.
As a matter of course, identifiers are not case-delicate. So you can utilize number or INTEGER to address a numeric worth. You can't utilize a saved watchword as an identifier.
The PL/SQL Delimiters
A delimiter is an image with an exceptional importance. Following is the rundown 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
Program remarks are logical explanations that can be remembered for the PL/SQL code that you compose and helps anybody perusing its source code. All programming dialects permit some type of remarks.
The PL/SQL underpins single-line and multi-line remarks. All characters accessible inside any remark are overlooked by the PL/SQL compiler. The PL/SQL single-line remarks start with the delimiter - (twofold hyphen) and multi-line remarks are encased by/* and */.
DECLARE
-- variable declaration
message varchar2(20):= 'Hello, World!';
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line(message);
END;
/
At the point when the above code is executed at the SQL brief, it creates the accompanying outcome −
Hello World
PL/SQL procedure successfully completed.
PL/SQL Program Units
A PL/SQL unit is any of the accompanying −
- PL/SQL block
- Capacity
- Bundle
- Bundle body
- Methodology
- Trigger
- Type
- Type body
Every one of these units will be talked about in the accompanying parts.