24
PL / SQL Faisal Anwer & Mohammad Nadeem 1

PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

PL / SQL

Faisal Anwer & Mohammad Nadeem

1

Page 2: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

DISADVANTAGES OF SQL

SQL does not have any procedural capabilities:

Condition checking

Looping and branching

There is no provision for exception handling.

To overcome these problems and provide some additional

benefits, PL/SQL was developed.

F28

DM

2

Ind

exe

s in

Ora

cle

Page 3: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

PL/SQL

PL/SQL is a programming language that supports SQL

data manipulation. But also provides:

facilities of conditional checking, looping and

branching.

provides exception handing facility.

supports variables to handle intermediate results.

and much more.

It is a superset of SQL.

F28

DM

3

Ind

exe

s in

Ora

cle

Page 4: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

PL/SQL GENERIC BLOCK

PL/SQL blocks contain three sections

Declare section

Executable section

Exception-handling section

The executable section is the only mandatory section of the

block.

Both, the declaration and exception-handling sections are

optional.

F28

DM

4

Ind

exe

s in

Ora

cle

Page 5: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

PL/SQL GENERIC BLOCK

F28

DM

5

Ind

exe

s in

Ora

cle

Page 6: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

VARIOUS SECTIONS

Declare Section:

Variables and objects can be declared and initialized (if required) in

this section.

Once declared, they can be used in SQL statements.

Begin Section:

It consists of a set of SQL and PL/SQL statements.

Data manipulation, retrieval, looping and branching constructs are

specified in this section.

Exception Section:

Handles the errors that arise during execution on PL/SQL

statements.

F28

DM

6

Ind

exe

s in

Ora

cle

Page 7: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

F28

DM

7

Ind

exe

s in

Ora

cle

Page 8: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

INPUT / OUTPUT IN PL/SQL

Input:

n1 := &n1; (for command line inputting)

n1 := :n1; (for GUI inputting)

Output:

dbms_output.put_line(‘Your message');

dbms_output.put_line(‘ ‘) displays a message string.

If the Output is OFF and it can be made ON by:

SET SERVEROUTPUT ON;

F28

DM

8

Ind

exe

s in

Ora

cle

Page 9: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

F28

DM

9

Ind

exe

s in

Ora

cle

Page 10: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

F28

DM

10

Ind

exe

s in

Ora

cle

Page 11: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

BASIC DATA TYPES

Number(p,s)

Char(n)

Varchar2(n)

Date

Boolean

%Type:

Declares a variable to have the same data type as that

of a previously defined variable or of a column in a

table.

Ex- Cname Client_Master.Name%Type;

F28

DM

11

Ind

exe

s in

Ora

cle

Page 12: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

LOGICAL COMPARISONS

>

<

=

<=

>=

<>

Logical operators:

AND

OR

NOT

F28

DM

12

Ind

exe

s in

Ora

cle

Page 13: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

CONDITIONAL STATEMENT

F28

DM

13

Ind

exe

s in

Ora

cle

Page 14: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE F

28

DM

14

Ind

exe

s in

Ora

cle

Page 15: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

LOOPING

Loop:

F28

DM

15

Ind

exe

s in

Ora

cle

Page 16: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

F28

DM

16

Ind

exe

s in

Ora

cle

Page 17: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

LOOPING

While loop:

F28

DM

17

Ind

exe

s in

Ora

cle

Page 18: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

F28

DM

18

Ind

exe

s in

Ora

cle

Page 19: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

LOOPING

For loop:

F28

DM

19

Ind

exe

s in

Ora

cle

Page 20: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

F28

DM

20

Ind

exe

s in

Ora

cle

Page 21: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

WHAT WE SAW TODAY

Generic PL/SQL Block

Various sections of a PL/SQL Block

Input/Output

Logical operators

Conditional Statement

Looping

Loop

While loop

For loop

F28

DM

21

Ind

exe

s in

Ora

cle

Page 22: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXAMPLE

Write a PL/SQL code block that will accept an account no

from user and deduct 100 Rs from it if its balance it less

than some minimum balance.

F28

DM

22

Ind

exe

s in

Ora

cle

Page 23: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

F28

DM

23

Ind

exe

s in

Ora

cle

Write a PL/SQL code block that will accept an account no from user and

deduct 100 Rs from it if its balance it less than some minimum balance.

Page 24: PL / SQL · Faisal Anwer & Mohammad Nadeem 1 . DISADVANTAGES OF SQL SQL does not have any procedural capabilities: Condition checking Looping and branching There is no provision for

EXERCISE

Enter a number. If the number is 1 then print ‘One’. If the

number is 2 then print ‘Two’. Else print ‘’Neither One nor

Two’.

Enter an year. Find out whether it is a leap year or not.

Print 10 odd numbers starting from 1.

F28

DM

24

Ind

exe

s in

Ora

cle