SQL PL/SQL Presented by: Dr. Samir Tartir

Slides:



Advertisements
Similar presentations
Oracle PL/SQL IV Exceptions Packages.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
PL/SQL Agenda: Basic PL/SQL block structure
Programming in Oracle with PL/SQL
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
PL/SQL.
Programming in Oracle with PL/SQL ProceduralLanguageExtension toSQL.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Advanced SQL: Cursors & Stored Procedures
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
PL/SQL Procedural Language / Structured Query Language.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Database Application Development using PL/SQL Programming.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Database Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Using SQL in PL/SQL ITEC 224 Database Programming.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
ITEC 224 Database Programming
Programming in Oracle with PL/SQL
Interacting with the Oracle Server
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
SQL Stored Triggers Presented by: Dr. Samir Tartir
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Handling Exceptions.
Normalization 2NF & 3NF Presented by: Dr. Samir Tartir
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
PL/SQL week10.
Session - 6 Sequence - 5 SQL Updating Database Contents
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Stored Procedures and Functions Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

SQL PL/SQL Presented by: Dr. Samir Tartir Session - 6 Sequence – 7 SQL PL/SQL Presented by: Dr. Samir Tartir

Outline Goals Types Structure

PL/SQL PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension language for SQL and the Oracle relational database. PL/SQL's general syntax resembles that of Ada. PL/SQL supports variables, conditions, loops and exceptions. PL/SQL blocks can include control flow and DML statements.

Benefits of Server-Side Code Can be used to include the business logic directly in the database. Code can be shared for all database users. Smaller network traffic, since code and data reside in the same location.

When is PL/SQL Useful? When something is too complicated for SQL When conditional branching and looping are needed

Types Runtime Code Stored Procedures Stored Functions Stored Packages Triggers 6

Basic Structure [DECLARE ... Local variables ]  Optional BEGIN ... PL/SQL Code [EXCEPTION ... Handlers]  Optional END; 7

Comments Single Line Multiple lines Anything in the line after the “--” is ignored -- …comment E.g: sal integer; -- store salary Multiple lines Anything between “/*” and “*/” is ignored. /* …comment… */ sal integer; /* this is A Multiline Comment */

Variables Variables can have: Examples: any SQL data type, such as CHAR, DATE, or NUMBER or any PL/SQL data type, such as BOOLEAN or BINARY_INTEGER. Examples: part_no NUMBER(4); in_stock BOOLEAN; 9

Assigning Values to Variables Using the assignment operator (:=) E.g.: tax := price * tax_rate; Selecting database values into it SELECT salary * 0.10 INTO bonus FROM employee WHERE SSN = SSN_In; Passing it as an OUT or IN OUT parameter when calling a procedure adjust_salary(7788, my_sal); 10

Flow Control – If Statement IF acct_balance >= debit_amt THEN UPDATE accounts SET bal = bal - debit_amt WHERE account_id = acct; ELSE INSERT INTO temp VALUES (acct, acct_balance, 'Insufficient funds'); END IF; 11

Flow Control – Case Statement CASE WHEN shape = 'square' THEN area := side * side; WHEN shape = 'circle' THEN BEGIN area := pi * (radius * radius); END; WHEN shape = 'rectangle' THEN area := length * width; ELSE DBMS_OUTPUT.PUT_LINE('No formula'); RAISE PROGRAM_ERROR; END CASE; 12

For Loops It lets you specify a range of integers, then execute a sequence of statements once for each integer in the range. FOR num IN 1..500 LOOP INSERT INTO roots VALUES (num, SQRT(num)); END LOOP; 13

While Loops Associates a condition with a sequence of statements. Before each iteration of the loop, the condition is evaluated. If the condition is true, the sequence of statements is executed, then control resumes at the top of the loop. If the condition is false or null, the loop is bypassed and control passes to the next statement. 14

Example WHILE salary <= 2500 LOOP SELECT salary, mgr_ssn, lname INTO salary, mgr_ssn, last_name FROM employee WHERE ssn = mgr_ssn; END LOOP; 15

Cursors A cursor is a pointer to a private SQL area that stores results of a SELECT statement Example: DECLARE CURSOR c1 IS SELECT empno, ename, job FROM emp WHERE deptno = 20; 16

Cursors Operations OPEN FETCH CLOSE 17

Cursor Variables cursorname%ROWCOUNT Rows returned so far cursorname%FOUND One or more rows retrieved cursorname%NOTFOUND No rows found Cursorname%ISOPEN Is the cursor open 18

Example – Using a For Loop SET SERVEROUTPUT ON DECLARE CURSOR c1 is SELECT fname, ssn, salary FROM employee ORDER BY salary DESC; --start w/ highest paid emp my_ename VARCHAR2(10); my_empno CHAR(9); my_sal NUMBER(10,2); BEGIN OPEN c1; FOR i IN 1..5 LOOP FETCH c1 INTO my_ename, my_empno, my_sal; EXIT WHEN c1%NOTFOUND; /* in case the number requested */ /* is more than the total */ /* number of employees */ DBMS_OUTPUT.PUT_LINE (MY_EMPNO||','||MY_SAL); END LOOP; CLOSE c1; END; /

Example – Using a While Loop SET SERVEROUTPUT ON DECLARE CURSOR c1 is SELECT fname, ssn, salary FROM employee ORDER BY salary DESC; --start w/ highest paid emp my_ename VARCHAR2(10); my_empno CHAR(9); my_sal NUMBER(10,2); BEGIN OPEN c1; FETCH c1 INTO my_ename, my_empno, my_sal; WHILE C1%FOUND LOOP DBMS_OUTPUT.PUT_LINE (MY_EMPNO||','||MY_SAL); END LOOP; CLOSE c1; END; /

Exception Handling Here we define the actions that should happen when an exception is thrown. Example Exceptions: NO_DATA_FOUND TOO_MANY_ROWS ZERO_DIVIDE User-defined exceptions

Example DECLARE num_row number_table%ROWTYPE; BEGIN select * into num_row from number_table; dbms_output.put_line(1/num_row.num); EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('No data!'); WHEN TOO_MANY_ROWS THEN dbms_output.put_line('Too many!'); WHEN OTHERS THEN dbms_output.put_line(‘Error’); end;

Example Create a new table: Employee2 For each employee Fname, LName, SSN, Salary, Salary2, Dsc For each employee If the employee worked more than 40 hours on all projects Put “Good” in his description and give him a 10% raise Otherwise: Put “Bad” in his description Find the employee with the highest salary and print his name and SSN Display the name and salary of employee with SSN “111997788”. If no employee has that SSN, display “Employee not found”

SUMMARY Oracle uses PL/SQL to include business logic right in the database directly. PL/SQL has many PL features to facilitate code writing Code can be Procedures, functions, packages or triggers

Resources & References Dr. Samir Tartir Email: startir@philadelphia.edu.jo Website: http://www.philadelphia.edu.jo/academics/startir Fundamentals of Database Systems by El Masri & Navathe. Publisher : Addison-Wesley, 5th edition, 2006.