pl / sql · faisal anwer & mohammad nadeem 1 . disadvantages of sql sql does not have any...

Post on 18-Jul-2020

10 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 exception handling.

To overcome these problems and provide some additional

benefits, PL/SQL was developed.

F28

DM

2

Ind

exe

s in

Ora

cle

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

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

PL/SQL GENERIC BLOCK

F28

DM

5

Ind

exe

s in

Ora

cle

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

EXAMPLE

F28

DM

7

Ind

exe

s in

Ora

cle

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

EXAMPLE

F28

DM

9

Ind

exe

s in

Ora

cle

EXAMPLE

F28

DM

10

Ind

exe

s in

Ora

cle

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

LOGICAL COMPARISONS

>

<

=

<=

>=

<>

Logical operators:

AND

OR

NOT

F28

DM

12

Ind

exe

s in

Ora

cle

CONDITIONAL STATEMENT

F28

DM

13

Ind

exe

s in

Ora

cle

EXAMPLE F

28

DM

14

Ind

exe

s in

Ora

cle

LOOPING

Loop:

F28

DM

15

Ind

exe

s in

Ora

cle

EXAMPLE

F28

DM

16

Ind

exe

s in

Ora

cle

LOOPING

While loop:

F28

DM

17

Ind

exe

s in

Ora

cle

EXAMPLE

F28

DM

18

Ind

exe

s in

Ora

cle

LOOPING

For loop:

F28

DM

19

Ind

exe

s in

Ora

cle

EXAMPLE

F28

DM

20

Ind

exe

s in

Ora

cle

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

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

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.

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

top related