INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
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 SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
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.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
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 (Embedded SQL) Introduction Benefits Basic Constructs
PL/SQL Agenda: Basic PL/SQL block structure
Programming in Oracle with PL/SQL
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
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.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
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.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
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.
Triggers and Stored Procedures in DB 1. Objectives Learn what triggers and stored procedures are Learn the benefits of using them Learn how DB2 implements.
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.
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.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
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:
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
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 Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
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 Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Creating Stored Procedures and Functions
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQL MULTIPLE CHOICE QUESTION.
SQL PL/SQL Presented by: Dr. Samir Tartir
PL/SQL week10.
Chapter 8 Advanced SQL Pearson Education © 2009.
Database Programming Using Oracle 11g
Presentation transcript:

INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL PL/SQL is an extension to SQL. Pl/SQL addresses the non-procedural nature of SQL. Programs written in PL/SQL are called subprograms. The three different types of subprograms are: Procedure: A procedure is a program that performs a specific function. Function: Is similar to a procedure but a function has a return clause. Trigger: A trigger is a stored program that is associated with a specific table and is invoked when a specific event occurs. Triggers accept no arguments.

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Program Structure

Declare Section Define the type of PL/SQL routine – procedure, function or trigger Define program variables Body Section Between Begin and Exception Clause Main section of the program Exception Section Between the Body Section and the END clause Handles program errors

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Declare Section DEFINING NUMBER VARIABLES counter BINARY_INTEGER; total_cost NUMBER(10,2); seconds_per_day CONSTANT NUMBER := 60 * 60 * 24; final_cost NUMBER(11,0) :=0; DEFINING CHARACTER VARIABLES first_name VARCHAR2(15) NOT NULL := ‘BROWN’; middle_initial VARCHAR2; company_name CONSTANT VARCHAR2(6) := ‘IBM’;

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Declare Section DEFINING DATE VARIABLES hire_date DATE :=‘01-FEB-96’; raise_date DATE; DEFINING BOOLEAN VARIABLES over_weight BOOLEAN NOT NULL :=TRUE absent BOOLEAN := NULL;

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section IF-THEN-ELSE EXAMPLE DECLARE num_jobs NUMBER(8); actor_id NUMBER(4) := 1111; BEGIN SELECT COUNT(*) INTO num_jobs FROM auditions WHERE actorid = actor_id AND called_back = ‘YES’; IF num_jobs > 90 THEN UPDATE actor SET actor_rating = ‘OSCAR winner’ WHERE actorid=actor_id; ELSIF num_jobs > 75 THEN UPDATE actor SET actor_rating = ‘Daytime TV’; ELSE UPDATE actor SET actor_rating = ‘Dish washer’ WHERE actorid = actor_id; END IF; COMMIT; END;

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section LOOP EXAMPLE DECLARE countr number(4) := 0; BEGIN LOOP INSERT INTO my_table VALUES (countr, ‘THE COUNT IS FINISHED’); countr = countr + 1; IF countr = 66 THEN EXIT; END IF; END LOOP; END;

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section CURSOR & LOOP EXAMPLE DECLARE g_total NUMBER(5); sal_top_limit CONSTANT NUMBER(5) :=89000; CURSOR x1 SELECT ename FROM emp WHERE sal > sal_top_limit; BEGIN OPEN x1; program body. CLOSE x1; END;

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section CURSOR & LOOP EXAMPLE 1 LOOP FETCH the_cursor INTO my_name, my_comm; EXIT WHEN the_cursor%NOTFOUND; ---- more program processing END LOOP; CURSOR & LOOP EXAMPLE 2 FETCH the_cursor INTO my_name, my_sal; WHILE the_cursor%FOUND LOOP more program processing FETCH the_cursor INTO my_name, my_sal; END LOOP;

INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Compiling And Executing SQL > set serveroutput on; - This allows SQL*PLUS to send the PL/SQL output to the screen SQL - This will compile the procedure or function SQL > show errors; - Shows compile errors SQL > execute pct_system; - This will run the procedure. SQL> drop procedure pct_system; - This will drop/remove the procedure from the database

INTRODUCTION TO ORACLE LANGUAGES PL/SQL Data Dictionary Views Include: DBA_SOURCE – Stores source code for procedures and functions DBA_ERRORS – Stores compiler error messages. After compiling a program error messages can also be displayed from SQL*PLUS using: SQL > show errors DBA_TRIGGERS - Stores source code, events and other triggers attributes

INTRODUCTION TO ORACLE LANGUAGES PL/SQL Functions create or replace function myencryptf( …….PROGRAM VARIABLES………… BEGIN dbms_obfuscation_toolkit.DESEncrypt(input_string => input_s, key_string => key_s, encrypted_string => encrypted_s ); RETURN encrypted_s; EXCEPTION WHEN error_in_input_buffer_length THEN dbms_output.put_line('> ' || INPUT_BUFFER_LENGTH_ERR_MSG); dbms_output.put_line('done '); END; /

INTRODUCTION TO ORACLE LANGUAGES Compiling a PL/SQL Function Function created. SQL> insert into emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) 2 values 3* (1111,myencryptf('asdfghjk'),'clerk',2222,sysdate,100.00,10.0,10); 1 row created. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO n‰?þq clerk JUL

INTRODUCTION TO ORACLE LANGUAGES SQL> select ename from emp; ENAME n‰?þq Function created. SQL> select mydecryptf(ename) from emp; MYDECRYPTF(ENAME) asdfghjk

INTRODUCTION TO ORACLE LANGUAGES Triggering Events Include: INSERT/UPDATE/DELETE Types Of Triggers Include: Row Level Trigger - Fires once for each affected row Statement Level Trigger - Fires once for the triggering event (FOR EACH ROW) Triggers must be compiled SQL - This will compile the trigger.

INTRODUCTION TO ORACLE LANGUAGES Row Level Trigger: CREATE OR REPLACE trigger db_trig AFTER INSERT OR UPDATE OR DELETE ON test_table1 DECLARE ….. Statement Level Trigger: CREATE OR REPLACE trigger db_trig AFTER INSERT OR UPDATE OR DELETE ON test_table1 FOR EACH ROW DECLARE ……