0% found this document useful (0 votes)
41 views26 pages

Session 17

Here is the solution to the exercise: DECLARE V_NUM NUMBER(2); V_SQUARE NUMBER(3); V_CUBE NUMBER(4); V_DOUBLE NUMBER(3); BEGIN V_NUM := # V_SQUARE := V_NUM * V_NUM; V_CUBE := V_SQUARE * V_NUM; V_DOUBLE := V_NUM * 2; DBMS_OUTPUT.PUT_LINE('NUMBER: ' || TO_CHAR(V_NUM)); DBMS_OUTPUT.PUT_LINE('SQUARE: ' || TO_CHAR(V_SQUARE)); DBMS_OUTPUT.PUT_LINE('CUBE:

Uploaded by

K V D Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views26 pages

Session 17

Here is the solution to the exercise: DECLARE V_NUM NUMBER(2); V_SQUARE NUMBER(3); V_CUBE NUMBER(4); V_DOUBLE NUMBER(3); BEGIN V_NUM := # V_SQUARE := V_NUM * V_NUM; V_CUBE := V_SQUARE * V_NUM; V_DOUBLE := V_NUM * 2; DBMS_OUTPUT.PUT_LINE('NUMBER: ' || TO_CHAR(V_NUM)); DBMS_OUTPUT.PUT_LINE('SQUARE: ' || TO_CHAR(V_SQUARE)); DBMS_OUTPUT.PUT_LINE('CUBE:

Uploaded by

K V D Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

PL/SQL – Part 1

Contents
• Lecture 1
• Why PL/SQL
• Basics

• Lectures 2 and 3
• Advanced PL/SQL

2
Limits of SQL
Bank clerk
Wants to do withdrawal transaction for a person for
some amount.

If the person has enough money in the account, then


update the account

If the person does not have money, then give a


message

Accounts (holder, balance, account_type)

3
An IF Statement in SQL

ACCEPT person PROMPT ‘Enter Persons Account Number: '


ACCEPT amount PROMPT ‘Enter Amount to withdraw : '

UPDATE Accounts
SET balance = balance - &amount
WHERE holder = '&person' AND balance > &amount;

SELECT 'Insufficient Funds'


FROM Accounts
WHERE holder = '&person' AND balance <= &amount;

4
SQL Limitation
• Cannot express business logic well

• Has to perform 2 actions whereas we only needed to


do one action

• Limited in our messaging – what happens if account


does not exist ? . . .

• SQL needs programming power !


• PL/SQL provides that extra power
5
PL/SQL – Programming
Language
• Oracle’s extension to SQL
• Proprietary Language

• Supports
• Variables and constants
• Structured data
• Assignment statements
• Loops
• Conditional
• Error handling
• Integrated with SQL

6
Example in PL/SQL
PROCEDURE withdrawal(person IN varchar(20),
amount IN REAL ) IS
current REAL;
BEGIN
SELECT balance INTO current
FROM Accounts
WHERE holder = person;
IF (amount > current) THEN
dbms_output.put_line('Insufficient Funds');
ELSE
UPDATE Accounts
SET balance = balance - amount
WHERE holder = person AND balance > amount;
COMMIT;
END IF;
END;

http://www.cse.unsw.edu.au/~cs3311/98s2/lec/plsql/notes.html
7
PROCEDURE withdrawal(person IN varchar(20),
Example in PL/SQL
amount IN REAL
current REAL;
) IS

BEGIN
SELECT balance INTO current
FROM Accounts
WHERE holder = person;
IF (amount > current)
dbms_output.put_line('Insufficient Funds');
ELSE
UPDATE Accounts
SET balance = balance - amount
WHERE holder = person AND balance > amount;
COMMIT;
END IF; Package and execute as
END; functional unit

SQL> EXECUTE withdrawal( ‘Gillian‘ , 1000000.00 );


8
Blocks
• PL/SQL is block-structured, where a block consists
of:
DECLARE
declarations for constants, variables and local procedures
BEGIN
procedural and SQL statements
EXCEPTION
exception handlers
END;

9
Hello World – Anonymous Block

SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE (‘Hello’);
END;
/

10
Hello World - Procedure

CREATE OR REPLACE PROCEDURE hello_world as


BEGIN
DBMS_OUTPUT.PUT_LINE (‘hello world’);
END;
/
11
Hello with some input

12
Program Structure
• May span multiple text editor lines

• Each line ends with a semicolon

• Text is not case sensitive

• Comments /* */

• Single line comments --


13
Scalar Data Types
• Atomic Data Types
• CHAR
• Fixed length characters (1 – 32, 767)
• VARCHAR2
• Variable length
• NUMBER
• Fixed decimal, integers, floating decimal
• BOOLEAN
• Logical – TRUE, FALSE, NULL
• DATE
• Others – BLOB, CLOB etc

Composite Data Types – RECORDS, TABLES


14
DECLARATIONS
• Variables and constants declared with a type
• Constants and not nulls must be initialized with a
value

DECLARE
varname dataType [ NOTNULL ]
[:= initialvalue] | [DEFAULT initialvalue] ;

constantname constant datatype := value ;


15
Examples
DECLARE
amount INTEGER;
part_number NUMBER(4);
in_stock BOOLEAN;
owner_name VARCHAR2(20);

tax_rate CONSTANT REAL := 0.15;


max_credit CONSTANT REAL := 8000.00;

my_credit REAL := 3000.00;

DECLARE
v_num NUMBER (3);
v_sum NUMBER (2) := 0;
v_major VARCHAR2 (5) DEFAULT ‘CIS’;
c_pi CONSTANT NUMBER :=3.14;
v_date DATE DEFAULT sysdate;
16
Printing in PL/SQL
SET SERVEROUTPUT ON SIZE buffer_size;

SQL> SET SERVEROUTPUT ON SIZE 4000;

SQL> SET SERVEROUTPUT ON ;

DECLARE
v_num NUMBER (2);

BEGIN
v_num := 50
v_double := v_num * 2;
DBMS_OUTPUT.PUT_LINE ( ‘Number is ‘ || TO_CHAR (v_num));
END;

17
Outputting

• DBMS_OUTPUT.PUT_LINE(string);

• TO_DATE: character string to DATE


• TO_DATE(‘07/14/01’, ‘MM/DD/YY’);
• TO_NUMBER: character string to NUMBER
• TO_NUMBER(‘2’);
• TO_CHAR: NUMBER or DATE to character string
• TO_CHAR(2);
• TO_CHAR(SYSDATE, ‘MM/DD/YYYY HH:MI’);
• String concatenation
• s_first_name || ‘ ’ || s_last_name

18
Program Control Statements
• Assignment statements
• Conditional Statements
• Loops

19
IF-THEN-END IF
• IF condition is true then action statements
performed, otherwise they are skipped
IF condition THEN
programstatements
END IF;

• IF/ELSE/END IF:
IF condition THEN
programstatements
ELSE
stmt-list-2
END IF;

20
IF-THEN-END IF /ELSE IF
IF (v_name = ‘dev’) THEN
v_salary = v_salary * 1.05 ;
v_commission := v_salary *.025 ;
END IF;

IF v_score >= 60 THEN


v_grade := `P` ;
ELSE
v_grade := `F`;
END IF;

21
IF THEN ELSIF END IF

IF v_position_id = 1 THEN
v_salary := v_salary * 1.2 ;
ELSIF v_position_id = 2 THEN
v_salary := v_salary * 1.3 ;
ELSIF v_position_id = 3 THEN
v_salary := v_salary * 1.35 ;
ELSIF v_position_id = 4 THEN
v_salary := v_salary * 1.4 ;
ELSIF
v_salary := v_salary * 2 ;
END IF;

22
Basic Loop LOOP
looping statements
EXIT [WHEN condition];
END LOOP;

BEGIN
v_sum := 100 ;
v_count := 1;
LOOP
v_sum := v_sum + v_count;
v_count := vcount + 1;
EXIT WHEN v_count > 10;
END LOOP;

What is v_sum and


v_count ?

23
WHILE Loop WHILE condition LOOP
statement-list
END LOOP;

BEGIN
v_sum := 100 ;
v_count := 1;
WHILE v_count <= 10
v_sum := v_sum + v_count;
v_count := vcount + 1;
END LOOP;

What is v_sum and


v_count ?

24
FOR Statement
FOR counter_variable
IN start_value .. end_value
LOOP
[REVERSE]
program statements
END LOOP;

v_sum := 0;
FOR counter in 1 .. 10 LOOP
v_sum := v_sum + v_count;
END LOOP

25
Exercise Complete exercise
Input a number and
write it out, as well as its
SQL> SET SERVEROUTPUT ON
square, cube and double
DECLARE
V_NUM NUMBER (2);
V_SQUARE NUMBER (3);
V_CUBE NUMBER (4);
V_DOUBLE NUMBER (3);
BEGIN
…. := &NUM;
V_SQUARE := V_NUM * V_NUM;
V_CUBE := V_SQUARE * ...
V_DOUBLE := ...
DBMS_OUTPUT.PUT_LINE
('NUMBER: ' || TO_CHAR (V_NUM));
...

END;

26

You might also like