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 − Show
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' ExampleDECLARE 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 − Hello World PL/SQL procedure successfully completed. The PL/SQL IdentifiersPL/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. 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 DelimitersA delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL −
The PL/SQL CommentsProgram 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. 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; / 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 UnitsA PL/SQL unit is any one of the following −
Each of these units will be discussed in the following chapters. What are the 3 sections of a Plsql block?As Figure 1-1 shows, a PL/SQL block has three parts: a declarative part, an executable part, and an exception-handling part. (In PL/SQL, a warning or error condition is called an exception.) Only the executable part is required. The order of the parts is logical.
How many of these PL SQL block are mandatory?A PL/SQL block has up to four different sections, only one of which is mandatory: Header.
Is declaration section mandatory in PL SQL?The declaration section is required if any variables are to be used in a PL/SQL block. The declaration section also defines cursors, types, local procedures, and functions that are used in a block. If no variables or other elements need to be declared, then this section may be omitted.
What are the different types of blocks in Plsql?In PL/SQL, All statements are classified into units that is called Blocks. PL/SQL blocks can include variables, SQL statements, loops, constants, conditional statements and exception handling.
|