PL sql1
PL sql1
Why PL-SQL ?
Language features
Basic Structure of PL/SQL
program
Data Types
Control Flow in PL-SQL
Loops in PL-SQL
Why PL SQL ?
Client
Server
SQL
SQL
SQL
Query1 Query2 Query3
PL-SQL Block
Client
Language features
PL SQL Block
Remember :
Declare is optional and only required when
variables need to be declared.
Exception is optional and required when
Error/Exception handling is done.
Begin and End are mandatory as all logic and
queries are written inside it.
Declare
Begin
Exception
End;
BEGIN
Insert into Dept values(70,HR,Pune);
Insert into Dept values(80,PSD,Mumbai);
Insert into Dept values(90,ESG,Pune);
END;
--This program will insert three records at the same time in the table dept.
To make changes:
Edit <File Name>
To edit program saved in folder other then bin
Edit C:\ESG\FirstPrg.Sql
To Execute:
@ File Name
To execute program saved in folder other then bin.
@ C:\ESG\FirstPrg.Sql
Important Keywords
DECLARE
BEGIN
END
EXCEPTION
LOOP , END LOOP
IF , ELSE , ELSIF , END IF
CURSOR
PROCEDURE
FUNCTION
Cont..
Important Keywords
Keywords
PACKAGE
TRIGGER
GRANT
REVOKE
FOR
WHILE
CASE
Operators
Airthmetic :
( + , - , * , /)
Logical:
(AND , OR , NOT)
Comparison:
(<=, <, >, =)
Comments (Two hyphens):
-Assignment operator: In PL SQL assignment
operator is
:=
So to assign values we need to write :=
Examples:
z := x + y
z := x
z := 100
name := MBT
Operators
;
||
&
Power
2**3 means 2 raise to power 3
**
In loop we use
Example:
For X in 1..5 means
1 to 5
..
Accept a value
Examples:
num1 := &Number1;
At run time this will prompt as
Follows
Accept a value
Examples:
name := &Name;
At run time this will prompt as
Follows
Display value
Dbms_output.put (Mahindra);
Dbms_output.put (British);
Dbms_output.put (Telecom);
Dbms_output.put_line( );
It will display Mahindra British Telecom on same
line.
Note :
1.
SET SERVEROUTPUT ON
2.
Dbms_output.put_line (Mahindra);
Dbms_output.put_line (British);
Dbms_output.put_line (Telecom);
It will display
Mahindra
British
Telecom
on different lines.
Note :
1.
SET SERVEROUTPUT ON
Examples
Sample 1: Sample1.SQL
Sample2:
Sample2.SQL
Char
CHAR datatype to store fixed-length character data.
Maximum size = 2000 bytes
Varchar2
VARCHAR2 datatype to store variable-length character
Number
Number types let you store numeric data (integers, real
numbers, and floating-point numbers), represent quantities,
and do calculations.
Binary_Integer
The BINARY_INTEGER datatype to store signed
integers (-2**31 to 2**31)
Date
DATE datatype to store fixed-length datetimes
Long
The LONG datatype to store variable-length character
strings. The LONG datatype is like the VARCHAR2
datatype, except that the maximum size of a LONG
value is 32760 bytes.
NChar
To store multi byte fixed length character data. Its same
as Char only difference is it is used to store characters
of different language like Japenese , chinese etc.
Number of characters it can store depend on language.
NVarchar
To store multi byte variable length character data. Its
same as Varchar2 only difference is it is used to store
characters of different language like Japenese , chinese
etc.
Number of characters it can store depend on language.
Record
Table
Its like Array in C Language. To be discussed in detail in
Second day session.
This Array type is un-constrained array
VArray
%RowType
Variable Name TableName%RowType
v_emp Emp%RowType;
Advantages of declaring in above way.
Examples
Examples 3 and 4 are same as sample 1 and
sample 2 but here we are using variable
declaration style of %Type
Sample 3:
Sample3.SQL
Sample4:
Sample4.SQL
Sample5:
Sample5.SQL
Sample6:
Sample6.SQL
Sample7:
Sample7.SQL
Conditional Statements
IF Then ELSE
If <condition1> Then
<Code>
ELSIF <Condition2> Then
<Code>
ELSE
<Code>
END IF;
Note here that for one IF we only need one END IF;
No END IF is required for ELSIF i.e for one set of IF
condition only one END IF; is required
Conditional Statements
IF Then ELSE
If v_deptno = 10 Then
DBMS_OUTPUT.PUT_LINE ('Accounting');
DBMS_OUTPUT.PUT_LINE (ESG');
ELSE
DBMS_OUTPUT.PUT_LINE (Invalid');
END IF;
Conditional Statements
Conditional Statements
CASE :
CASE
When v_deptno =10 Then
DBMS_OUTPUT.PUT_LINE ('Accounting');
DBMS_OUTPUT.PUT_LINE (ESG');
DBMS_OUTPUT.PUT_LINE (Invalid');
END CASE;
Examples
Sample8:
Sample8.SQL
Sample9:
Sample9.SQL
Sample10:
Sample10.SQL
Sample11:
Sample11.SQL
TYPES OF LOOPS
Simple Loop
Loop
Exit When <Condition>
<Code>
End Loop;
TYPES OF LOOPS
Simple Loop
Loop
Exit When i = 10
dbms_output.put_line (i);
End Loop;
--Pre Tested
TYPES OF LOOPS
Simple Loop
Loop
<Code>
Exit When <Condition>
End Loop;
TYPES OF LOOPS
Simple Loop
Loop
dbms_output.put_line (i);
Exit When i = 10
End Loop;
--Post Tested
TYPES OF LOOPS
While Loop
While <Condition>
Loop
<Code>
End Loop;
TYPES OF LOOPS
While Loop
While i < 10
Loop
dbms_output.put_line (i);
End Loop;
TYPES OF LOOPS
FOR Loop
FOR <Variable> IN <Min> .. <Max>
Loop
<Code>
End Loop;
TYPES OF LOOPS
FOR Loop
FOR i IN 1 .. 100
Loop
<Code>
End Loop;
TYPES OF LOOPS
Examples
Sample12:
Sample12.SQL
Sample13:
Sample13.SQL
Sample14:
Sample14.SQL
Sample15:
Sample15.SQL
Thank you !!
Next Session : Tomorrow
Cursor ,Record and Exception