Mastering PL/SQL : Syntax, Identifiers , Comments , Declaration , Datatypes (Part-1) .
What PL/SQL ?
PL/SQL is nothing but a combination of SQL along with the procedural features of programming language. It means that in SQL we run query one at a time but in PL/SQL we make a program in which we use SQL and that program works similar like you all must have seen in C,C++ or Java.
SYNTAX
Basic Syntax of PL/SQL which is a block-structured language; this means that PL/SQL programs are written in logical block of code. Each block of code consist of three subparts :-
DECLARE - This section starts with the keyword DECLARE . Its an option section and defines variables , cursors , subprogram and other elements to be used in the program.
BEGIN - This section starts with the keyword BEGIN . It consists of 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. In layman's language , in the block of BEGIN we have to write our main code which we want to execute like we do in C language after declaration of variables we write the main code . If you don't understand just be cool and read further .
EXCEPTION - This section starts with the keyword EXCEPTION . This optional section contain exceptions that handle errors in the program.
END - This section starts with the keyword END . It is used at the end of the program.
DECLARE
message varchar2(20) := 'Hello World';
BEGIN
dbms_output.put_line(message);
END;
This is the basic syntax of PL/SQL.
Mostly used Data types of PL/SQL๐
These are the few data types which often used in PL/SQL Programming.
How to Declare a Variable?
Here in this example ๐๐ Sales(10,2) is a variable which has Data Type Number and that variable can hold maximum 10 digit number and decimal before 2nd last digit.
Let's understand with some more example.๐๐
Here in this example ๐๐ f is a variable which can store a real number that's why value of f is resulted in real.
ADD program in PL/SQL
DECLARE
var1 number(5);
var2 number(3);
var3 number(4);
BEGIN
var1:=4;
var2:=66;
var3:=var1+var2;
dbms_output.put_line('The Output is = '||var3);
END;
Understanding the above program ๐๐
In the DECLARE section I declared 3 variables with name var1 , var2 , var3 with length 5 ,3 ,4 it means var1 can hold a maximum 5 digit number and same concept goes with var2 & var3. In the next begin section I assigned value of 4 in var1 , 66 in var2 , added var2 & var1 in var3 in the 2nd last line I printed var3 and ended the program.
Which are Identifiers in PL/SQL?
PL/SQL identifiers are constants , cursors , variables , exceptions , procedures and reserved words. The identifiers consist of letters , numerals , dollar signs , under scores and number signs and should not exceed 30 characters.
How to Comment in PL/SQL ?
Single line comment - use delimiters -- (double hyphen).
Multil line comment - these comments are enclosed by / /
<------------END-------------->
Subscribe to my newsletter
Read articles from Sahitya Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by