CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
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.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Chapter 4B: More Advanced PL/SQL Programming
PL/SQL Agenda: Basic PL/SQL block structure
Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.
Programming in Oracle with PL/SQL
PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental.
Introduction to PL/SQL
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
PL/SQL Introduction Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
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.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Chapter 4: Introduction to PL/SQL
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
Advanced SQL: Cursors & Stored Procedures
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
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.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Using SQL in PL/SQL ITEC 224 Database Programming.
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.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
DB Programming Course Lecture 4. Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is a special section.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
Handling Exceptions.
SQL PL/SQL Presented by: Dr. Samir Tartir
Agenda Summary of last class Cursors Loops Records Simple Loops
PL/SQL – Session 1 of 4 Instructor: Jim Cody
Advanced PL/SQL Programing
Handling Exceptions.
Chapter 4: Introduction to PL/SQL
PL/SQL Declaring Variables.
Chapter 8 Advanced SQL Pearson Education © 2009.
Presentation transcript:

CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman

What is PL/SQL ? Oracle’s procedural extension to SQL superset of the SQL language, including high- level programming features such as: –structures, types –variables –constants –assignment statements –conditional statements –loops –customized error handling –structured data

Data types and variables all the sql data types –variables declaration: [not null] [:= ] ex. sid number(5) not null := 1111; sname varchar2(30); grade real(5,2) := 2.5; – constants declaration: constant := ex. maxcolumns constant integer(2) := 30; – anchor variables: %type [not null] [:= ] ex. cnum customers.sid%type; ctable customers%rowtype; -- creates a variable of type table that has the same fields as the customers table;

Comments Single line comments : “--” Block comments : C like “/* this is a comment */”

Assignments := ex. i:=i+1; sname := ‘Jones’; sales := price*qty;

Example In SQLPLUS run the following command: – set SERVEROUTPUT on DECLARE i INTEGER; sid NUMBER(5) NOT NULL := 1111; sname VARCHAR2(30); grade REAL(5) := 12.5; MAXCOLUMNS CONSTANT INTEGER(2) := 30; BEGIN i := 35; sname := 'Jones'; sid := 2000; DBMS_OUTPUT.PUT_LINE('i = ' || i); DBMS_OUTPUT.PUT_LINE('sid = ' || sid); DBMS_OUTPUT.PUT_LINE('sname = ' || sname); DBMS_OUTPUT.PUT_LINE('grade = ' || grade); DBMS_OUTPUT.PUT_LINE('MAXCOLUMNS = ' || MAXCOLUMNS); END; /

Conditional statements if-then –if then end if; ex. if (grade > 70) and (grade <90) then i:=i+1; end if; if-then-else –if then else end if; if-then-elseif –if then elseif then … elseif then else endif;

Loops Basic loop – loop end loop; ex. loop i:=i+1; if (i>10) then exit; end if; sum := sum + i; end loop; Alternatively we can have exited with “exit when i >10”

Loops For loop for in [reverse].. loop ; end loop; ex. for i in loop sum := sum + i; end loop; While loop while loop ; end loop; ex. while (i<10) loop sum := sum +i; i := i+1; end loop;

Program structure Anonymous programs declare --type and variable declarations begin --executable section null; exception -- exception handlers when others then null; --default handler for all untreated exceptions end; Procedures and functions procedure (,.., ) is [declarations] begin --executable section; exception --exception handlers; end; where “ has the following syntax: [in | out | in out]

Functions and procedures Procedure : will not return a result Function : will return a value after execution ex. function myfunc( param1 IN number) return number is grade number; begin grade:=param1 ; return (grade); end; procedure myproc( param1 IN number, param2 out number) is begin param2:=param1; end;

How to call the function ? declare function myfunc( param1 IN number) return number is grade number; begin grade:=param1 ; return (grade); end; begin DBMS_OUTPUT.PUT_LINE('The function returned: ' || myfunc(10)); end; / Exercise : WRITE THE CODE FOR CALLING THE PROCEDURE

Use the select statement in PL/SQL (only if the select returns one single row as result ) declare name varchar2(100); id number; begin select sid, fname into id,name from students where sid = 1111; end; /

Cursors When the result of a select statement consists of more than one row the “select into” statement can not be used. A PL/SQL cursor allows a program to fetch and process information one row at a time Declaration: cursor is ;

Cursor example DECLARE CURSOR c1 IS select sid,fname from students; c1_rec c1%rowtype; BEGIN if not c1%isopen then open c1; end if; fetch c1 into c1_rec; while c1%found loop dbms_output.put_line('Row Number ' || c1%rowcount || '> ' || c1_rec.sid || ' ' || c1_rec.fname); fetch c1 into c1_rec; end loop; close c1; END; /

How to work with cursors declare the cursor declare a variable rec_name of type cursor%rowtype “open c_name” fetch row by row “fetch c_name into rec_name” “close cursor” –c_name%found – returns true if there are still records, false otherwise –c_name%isopen - returns true if the cursor is open, false otherwise

Cursor “for” example DECLARE CURSOR c1 IS select sid,fname from students; BEGIN for c1_rec in c1 loop dbms_output.put_line('Row Number ' || c1%rowcount || '> ' || c1_rec.sid || ' ' || c1_rec.fname); end loop; END; / When using “for loops” the cursor does not have to be explicitly opened and fetched from.

Stored Procedures Syntax create [or replace] procedure [( )] as begin --executable section [exception ] end

why needed ? most of the time the stored procedures contain the entire application logic Ex: create a report with all the courses on all the years, average grade of the curse, students enrolled in the course, their grades on all the components of the courses and their final grade.

Exceptions when an error occurs during the execution of a PL/SQL program a exception is raised program control is transferred to the exception section

Common exception NO_DATA_FOUND -- select into failed because the it resulted in no row TOO_MANY_ROWS -- select into failed because the it resulted more than one row INVALID_NUMBER -- to_number(string) has invalid input parameter ZERO_DEVIDE -- a division by 0 occured

Views A view is a named query, virtual table Views are created, dropped or granted access to, identical to a table.

How do views differ from tables? From :

Syntax create view as ; drop view ; ex. create view vCourses as select catalog.ctitle, courses.term, courses.lineno from catalog, courses where catalog.cno=courses.cno; select * from vCourses;

Sql Injection SELECT fieldlist FROM table WHERE field = '$ '; SELECT fieldlist FROM table WHERE field = 'anything' OR 'x'='x'; SELECT , passwd, login_id, full_name FROM members WHERE = 'x'; UPDATE members SET = WHERE = SELECT , passwd, login_id, full_name FROM members WHERE = 'x'; DROP TABLE members; - -';