PL SQL Introduction
PL SQL Introduction
What is PL-SQL
• Features of PL/SQL:
• PL/SQL is basically a procedural language, which provides
the functionality of decision making, iteration and many
more features of procedural programming languages.
• PL/SQL can execute a number of queries in one block using
single command.
What is PL-SQL
The PL/SQL programming language was developed by Oracle Corporation in the late 1980s
as procedural extension language for SQL and the Oracle relational database. Following
are certain notable facts about PL/SQL −
PL/SQL is a completely portable, high-performance transaction-processing language.
PL/SQL provides a built-in, interpreted and OS independent programming environment.
PL/SQL can also directly be called from the command-line SQL*Plus interface.
Direct call can also be made from external programming language calls to database.
PL/SQL's general syntax is based on that of ADA and Pascal programming language.
Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.
What is PL-SQL
• Disadvantages of SQL:
• SQL doesn’t provide the programmers with a technique of
condition checking, looping and branching.
• SQL statements are passed to Oracle engine one at a time
which increases traffic and decreases speed.
• SQL has no facility of error checking during manipulation of
data.
PL/SQL - Environment Setup
After downloading the above two files, you will need to unzip them in a single
directory database and under that you will find the following sub-directories and a setup
file.
Finish the setup
It is now time to verify your installation. At the command prompt, use the following command if
you are using Windows −
sqlplus "/ as sysdba"
You should have the SQL prompt where you will write your PL/SQL commands and scripts −
What is PL-SQL
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
PL/SQL Program Units
Integer INTEGER
Smallint SMALLINT
Numeric NUMERIC(P,S)
Real REAL
Decimal DECIMAL(P,S)
Float FLOAT(P)
Character CHAR(X)
Date Date
Time TIME
PL/SQL Datatpye
DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;
BEGIN
null;
END;
PL/SQL Variable
• Explanation:
• SET SERVEROUTPUT ON : It is used to display the buffer
used by the dbms_output.
• var1 INTEGER : It is the declaration of variable, named var1
which is of integer type. There are many other data types
that can be used like float, int, real, smallint, long etc. It also
supports variables used in SQL as well like NUMBER(prec,
scale), varchar, varchar2 etc.
• PL/SQL procedure successfully completed. : It is
displayed when the code is compiled and executed
successfully.
• Slash (/) after END; : The slash (/) tells the SQL*Plus to
execute the block.
Variable Scope in PL/SQL
PL/SQL allows the nesting of blocks, i.e., each program block may contain
another inner block. If a variable is declared within an inner block, it is not
accessible to the outer block. However, if a variable is declared and
accessible to an outer block, it is also accessible to all nested inner blocks.
There are two types of variable scope −
Local variables − Variables declared in an inner block and not accessible to
outer blocks.
Global variables − Variables declared in the outermost block or a package.
Global and Local variables
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
PL/SQL Variable
Constant declaration
Syntax
constant_name Constant datatype := value;
The word Constant is a reserved word and that value does not change.
value: value is assigned to constant when it is declared.
PL/SQL Constant
Example
declare
salary_bonus constant number(4):=2000;
begin
salary_bonus := 4000;
dbms_output.put_line (salary_bonus);
end;
PL SQL Constant
DECLARE
-- constant declaration
-- other declarations
radius number(5,2);
dia number(5,2);
BEGIN
-- processing
radius := 9.5;
dia := radius * 2;
PL/SQL Operator
Arithmetic Operators + - * / %
Relational Operators < > <= >=
Logical Operators AND OR NOT
Miscellaneous Operators
PL/SQL Operator
PLSQL : || Operator
The string in PL/SQL is actually a sequence of characters with an optional size
specification.
The characters could be numeric, letters, blank, special characters or a combination of
all.
The || Operator in PLSQL is used to concatenate 2 or more strings together.
The result of concatenating two-character strings is another character string.
The result has datatype CHAR and is limited to 2000 characters if both character
strings are of datatype CHAR whereas if either string is of datatype VARCHAR2, the
result has datatype VARCHAR2 and is limited to 4000 characters.
PL/SQL Operator
The CONCAT character function can also be used as an alternative to
the vertical bar operator in PLSQL for concatenation of strings.
Syntax:
string1 || string2 [ || string_n ]
Parameters Used:
DECLARE
Test_String string(10) := 'Hello ';
Test_String2 string(10) := 'world!';
BEGIN
dbms_output.put_line((Test_String || Test_String2));
END;
PL/SQL Operator
+ Addition a+b=11
- Subtraction a-b=5
* Multiplication a*b=24
/ Division a/b=2
• Relational Operators
Operators Description Example: a=10,
b=20
!= Check the left operands value are not equal to right operands, if yes return true. a!=b=True
> Check the left operand value is greater than right Operand, if yes condition becomes a>b=False
true
< Check the left operand value is less than right Operand, if yes condition becomes a<b=True
true
<= Check the left operand value is less than or equal to right Operand, if yes condition a<=b=True
becomes true
>= Check the left operand value is greater than or equal to right Operand, if yes a>b=False
condition becomes true
PL/SQL Operator
• Logical Operator
Operator Description
1 AND And are use to combined two or more than two condition together. If both the
condition is true then return true.
2 OR OR are use to combined two or more than two condition together, In this case you
need at least one condition is true then return result.
• Miscellaneous Operator
minus
PL/SQL Comment
• comment */
PL/SQL Input Output
Statements
Example to see how to display a message using
PL/SQL :
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
var varchar2(40) := 'I love Computers' ;
BEGIN
dbms_output.put_line(var);
END;
/
PL/SQL Conditional Statements
Example
DECLARE n_sales NUMBER :=
2000000;
BEGIN
IF n_sales > 100000 THEN
DBMS_OUTPUT.PUT_LINE( 'Sales
revenue is greater than 100K ' );
END IF;
END;
PL/SQL Conditional Statements
Instead, use:
IF b_profitable THEN
DBMS_OUTPUT.PUT_LINE( 'This sales deal is profitable' );
END IF;
PL/SQL Conditional Statements
set serveroutput on;
if...else
DECLARE
if (condition) then x int;
statements; BEGIN
else x := 20;
statements; if mod(x,2) = 0 then
end if; dbms_output.put_line('Even
Number');
else
dbms_output.put_line('Odd
Number');
end if;
END;
If..else example
DECLARE
x number := 10;
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 10;
exit WHEN x > 50;
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit x is: ' || x);
WHILE LOOP
While statement in PL/SQL programming language repeatedly executes a target statement as long as a
given condition is true.
Syntax
WHILE condition LOOP
sequence_of_statements
END LOOP;
Example
DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20 LOOP
dbms_output.put_line('value of a: ' || a);
For Loop in PL/SQL
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
Nested Loops
PL/SQL allows using one loop inside another loop. Following section shows a few examples to illustrate the concept.
The syntax for a nested basic LOOP statement in PL/SQL is as follows −
LOOP
Sequence of statements1
LOOP
Sequence of statements2
END LOOP;
END LOOP;
• Example
• DECLARE
• grade char(1) := 'A';
• BEGIN
• CASE grade
• when 'A' then dbms_output.put_line('Excellent');
• when 'B' then dbms_output.put_line('Very good');
• when 'C' then dbms_output.put_line('Well done');
• when 'D' then dbms_output.put_line('You passed');
• when 'F' then dbms_output.put_line('Better try again');
• else dbms_output.put_line('No such grade');
• END CASE;
• END; --enter grade: ‘A’, enter character in single quotes