Opening Hours :7AM to 9PM
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. |
DECLARE <declarations section> BEGIN <executable command(s)> EXCEPTION <exception handling>> END;
DECLARE message varchar2(20):= 'Hello, World!'; BEGIN dbms_output.put_line(message); END; /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 −
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 |
DECLARE -- variable declaration message varchar2(20):= 'Hello, World!'; BEGIN /* * PL/SQL executable statement(s) */ dbms_output.put_line(message); END; /When the above code is executed at the SQL prompt, it produces the following result −