PL/SQL Agenda: Basic PL/SQL block structure

Slides:



Advertisements
Similar presentations
PL/SQL.
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.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
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
From last class… PL/SQL –Triggers –Block structure –Anonymous vs. named blocks –Functions and procedures –Using PL/SQL from SQL*Plus.
PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs
Programming in Oracle with PL/SQL
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
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.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
PL/SQL Anonymous Blocks
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
Stored Procedures Functions Packages
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
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.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
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 Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
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 Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
1 Copyright © 2004, Oracle. All rights reserved. Introduction to 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.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Anonymous Block · Types of Block · Declaring a Variable · Constant.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
Using SQL in PL/SQL ITEC 224 Database Programming.
Chapter Sixteen Cursors Objective: – Introduction to cursors – Use of cursors in a database record – Implicit & explicit cursors – Cursors & loops – Cursors.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
ITEC 224 Database Programming PL/SQL Lab Cursors.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
DATABASE PROGRAMS CS 260 Database Systems. Overview  Introduction  Anonymous blocks  Oracle’s PL/SQL language basics  Conditions and loops  Cursors.
implicit and an explicit cursor
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
1 ITBIS373 Database Development Lecture 2 – Chapter 4B Introduction to PL/SQL.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
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.
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.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/ /5/10.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Difference between Oracle PL/SQL and MySQL
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
SQL PL/SQL Presented by: Dr. Samir Tartir
Agenda Summary of last class Cursors Loops Records Simple Loops
Handling Exceptions.
PL/SQL week10.
Chapter 8 Advanced SQL Pearson Education © 2009.
Presentation transcript:

PL/SQL Agenda: Basic PL/SQL block structure Example procedures and functions Executing PL/SQL from SQL*Plus Exceptions Packages

More PL/SQL - block structure IS BEGIN EXCEPTION END; -- defines type of block (procedure, function, anonymous) and the way it is called -- declare any variables used in the block -- use variables and other PL/SQL objects to perform actions -- handle any problems that arise during execution of the block Block Header Declaration Header REQUIRED Execution Section Exception Section

PL/SQL… Is not case sensitive Uses the same datatypes as SQL Also has boolean, record, table, varray and LOB Allows reference datatypes %type and %rowtype Allows comments /* and */ for multiline and -- for single line Uses := for assignment Uses ; to indicate end of line (instruction)

PL/SQL - example DECLARE loop_count BINARY_INTEGER := 0; BEGIN LOOP INSERT INTO count_table VALUES (loop_count); DBMS_output.put_line (‘loop_count is ’ || to_char(loop_count)); loop_count := loop_count + 1; EXIT WHEN loop_count = 6; END LOOP; END;

Scott.GIVE_RAISE CREATE OR REPLACE PROCEDURE give_raise ( p_deptno IN number, p_raise_percent IN number ) AS BEGIN update emp set sal = sal + (sal * p_raise_percent * .01) where deptno = p_deptno; commit; END give_raise;

Another example… CREATE OR REPLACE FUNCTION get_company_name (comp_id_in IN company.id%TYPE) RETURN varchar2 IS cname company.name%TYPE; BEGIN SELECT name INTO cname FROM company WHERE id = comp_id_in; RETURN cname; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN OTHERS DBMS_OUTPUT.PUT_LINE (‘error code= ‘ || SQLCODE); DBMS_OUTPUT.PUT_LINE (‘error msg= ‘ || SQLERRM); END;

Cursors Implicit Explicit Created automatically in Oracle Don’t need to be declared Can be used only when 1 and only 1 record is returned Explicit Declared in declaration section attributes specifying the state of the cursor %notfound, %found, %rowcount, %isopen Use “cursor for loop” to work with FOR item IN cursor LOOP … END LOOP; Substitute cursor name and row variable for cursor and item Opens cursor and fetches results into cursor automatically

Another PL/SQL example… DECLARE current_bldg_code VARCHAR2(5); CURSOR location_cursor IS SELECT room, capacity FROM location WHERE bldg_code = current_bldg_code FOR UPDATE of capacity; location_row location_cursor%rowtype; BEGIN current_bldg_code := ‘LIB’; FOR location_row IN location_cursor LOOP UPDATE location Set capacity = capacity +1 WHERE CURRENT OF location_cursor; END LOOP; COMMIT; END; From Morrison & Morrison, p. 247

Using PL/SQL - functions From SQL*Plus command line Functions require that you create a variable in SQL*Plus to hold the value returned first variable cname varchar2(100) then select get_company_name(1) from dual; valid commands to execute stored function f1 returns number) from SQL*Plus. exec :a := f1; execute :a := f1; call f1() into :a; begin :a := f1; end; select f1 from dual;

Using PL/SQL - procedures From SQL*Plus command line Procedures don’t return a value, so Execute Give_raise(20,10); valid commands to execute stored procedure p1 from SQL*Plus. exec p1; execute p1; call p1(); begin p1; end;

Debugging PL/SQL Remember the Oracle error message facility – will help figure out the problem Comment out lines that may be causing problems Use dbms_output.put_line to show variable values Fix from the top down – 1 error may cause multiple error messages Data dictionary views: user_errors, user_source

Exception types NO_DATA_FOUND TOO_MANY_ROWS DUP_VAL_ON_INDEX And more… Create your own

Another PL/SQL example… FUNCTION build_name (name_in IN VARCHAR2, sex_in IN VARCHAR2) RETURN VARCHAR2 IS unknown_sex EXCEPTION; name_out VARCHAR2(100); BEGIN IF sex_in =‘M’ THEN name_out := ‘Mr. ‘ || name_in; ELSIF sex_in =‘F’ THEN name_out := ‘Ms. ‘ || name_in; ELSE RAISE unknown_sex END IF; RETURN name_out; EXCEPTION WHEN unknown_sex THEN DBMS_OUTPUT.PUT_LINE (‘Unable to determine gender of individual!’); END;

Packages Structure of Packages Advantages of Packages package header vs. package body Advantages of Packages invalidated objects hiding details of code code maintainability

Oracle provided packages DBMS_output DBMS_job DBMS_pipe And more… Set serveroutput on size 4000; Dbms_output.put_line (‘text to be displayed’);