

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
PL/SQL is a procedural language designed specifically to embrace SQL statements within its syntax. PL/SQL program units are compiled by the Oracle Database server and stored inside the database. And at run-time, both PL/SQL and SQL run within the same server process, bringing optimal efficiency. PL/SQL automatically inherits the robustness, security, and portability of the Oracle Database.
Typology: Slides
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























CONTENTS
PL/SQL BLOCK
DECLARE - - (optional) /* Here you declare the variables you will use in this block / BEGIN - - (mandatory) / Here you define the executable statements (what the block DOES!)/ EXCEPTION - - (optional) / Here you define the actions that take place if an exception is thrown during the run of this block */ END; - - (mandatory)
DATA TYPE AND VARIABLES Use of Data-Types <variable-name>
Radius: 9. Diameter: 19 Circumference: 59. Area: 283. DECLARE pi constant number := 3.141592654; radius number(5,2); dia number(5,2); circumference number(7, 2); area number (10, 2); BEGIN radius := 9.5; dia := radius * 2 ; circumference := 2.0 * pi * radius; area := pi * radius * radius; dbms_output.put_line ('Radius: ' || radius); dbms_output.put_line ('Diameter: ' || dia); dbms_output.put_line ('Circumference: ' || circumference); dbms_output.put_line ('Area: ' || area); END; A Simple Program to Calculate the Circumference and Area of a Circle OUTPUT
DECLARE num number := 10 ; x num%type := 20 ; name employee.fname%type := 'Ahmad'; P1 project%rowtype; BEGIN dbms_output.put_line ('num: ' || num); dbms_output.put_line ('x: ' || x); dbms_output.put_line ('name: ' || name); SELECT Pname , Plocation INTO P1.pname , P1.plocation FROM project WHERE Pnumber= 10 ; dbms_output.put_line ('P1.Name: ' || P1.pname || CHR(10) || 'P1.Plocation: ' || P1.Plocation);
- - CHR(10) --->> New line
EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line (SQLERRM); WHEN TOO_MANY_ROWS THEN dbms_output.put_line (SQLERRM); END; num: 10 x: 20 name: Ahmad P1.Name: Computerization P1.pLocation: Stafford OUTPUT Example
Some PL / SQL - operators
DECLARE grade char(1) := :EnterGrade; BEGIN IF grade = 'A' THEN dbms_output.put_line('Excellent'); ELSIF grade = 'B' THEN dbms_output.put_line('Very good'); ELSIF grade = 'C' THEN dbms_output.put_line('Good'); ELSIF grade = 'D' THEN dbms_output.put_line('Fair'); ELSE dbms_output.put_line('Fail'); END IF; END; IF condition 1 THEN {...statements to execute when condition1 is TRUE...} ELSIF condition THEN {...statements to execute when condition2 is TRUE...} ELSE {...statements to execute when if all of the previous
. conditions are false...} END IF; PL/SQL If
PL/SQL Case Statement
dayOfWeek char(1) ; BEGIN SELECT TO_CHAR(SYSDATE, 'D') INTO dayOfWeek FROM DUAL; CASE dayOfWeek WHEN 1 THEN dbms_output.put_line('Today is Sunday'); WHEN 2 THEN dbms_output.put_line('Today is Monday'); WHEN 3 THEN dbms_output.put_line('Today is Tuesday'); WHEN 4 THEN dbms_output.put_line('Today is Wednesday'); WHEN 5 THEN dbms_output.put_line('Today is Thursday'); WHEN 6 THEN dbms_output.put_line('Today is Friday'); WHEN 7 THEN dbms_output.put_line('Today is Saturday'); ELSE dbms_output.put_line('Error'); END CASE; END;
WHEN condition_1 THEN result_ WHEN condition_2 THEN result_ WHEN condition_3 THEN result_ ELSE result END CASE;
PL/SQL Exit Loop (Basic Loop)
sumn number := 0 ; counter number := 1 ; BEGIN LOOP sumn := sumn + counter; counter := counter + 1 ; IF counter > 10 THEN EXIT; END IF; END LOOP; dbms_output.put_line ('The sum is :' || sumn); END;
LOOP statements; EXIT; {or EXIT WHEN condition;} END LOOP; The sum is : 55 OUTPUT
PL/SQL While Loop Syntax of while loop: - - Calculating even numbers between 1 and 100
counter number := 1 ; sumn number := 0 ; BEGIN WHILE counter <= 100 LOOP IF counter MOD 2 = 0 THEN sumn := sumn + counter; END IF; counter := counter + 1 ; END LOOP; dbms_output.put_line ('The sum is:' || sumn); END;
WHILE
PL/SQL Cursor PL/SQL cursor is a pointer that points to a result of a query. It contains information on a select statement and the rows of data accessed by it. A cursor is used to fetch and process the rows returned by the SQL statement, one at a time. There are two types of cursors: explicit and implicit.
**PL/SQL Cursor