PL/SQL.

Slides:



Advertisements
Similar presentations
Definition Cursor is a control structure for the successive traversal of records in a result set. Cursor is a record pointer in a database. DATABASE CURSOR.
Advertisements

8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
H.Melikian1 PLSQL Dr.Hayk Melikyan Departmen of Mathematics and CS
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
PL/SQL.
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Chapter 4B: More Advanced PL/SQL Programming
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.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
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 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
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.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
DATA MANIPULATION andCONTROL
Advanced SQL: Cursors & Stored Procedures
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
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.
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.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter Sixteen Cursors Objective: – Introduction to cursors – Use of cursors in a database record – Implicit & explicit cursors – Cursors & loops – Cursors.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
implicit and an explicit cursor
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
PL/SQL  PL/SQL stands for Procedural Language/SQL.  PL/SQL extends SQL by adding constructs found in procedural languages like procedures, loops, variables,
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL Triggers, Functions & Stored Procedures Programming Operations.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Active Database Concepts
Interacting with the Oracle Server
PL/SQL.
SQL Stored Triggers Presented by: Dr. Samir Tartir
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Database Management Systems 2
Database Management Systems 2
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Introduction to Triggers
Presentation transcript:

PL/SQL

PL/SQL CURSORS Oracle creates a memory area, called context area, for processing SQL statement, Contains all information needed for processing the SQL statement. A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds the rows returned by a SQL statement. Cursors allow to fetch and process rows returned by a SELECT statement.

PL/SQL CURSORS There are two types of cursors: Implicit cursors Explicit cursors Implicit Cursors Implicit cursors are automatically created by Oracle whenever an SQL statement is executed Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor is associated with this statement For INSERT operations, the cursor holds the data that needs to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would be affected.

PL/SQL CURSORS Attributes %FOUND Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows Otherwise, it returns FALSE. %NOTFOUND The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or DELETE statement affected no rows, Otherwise, it returns FALSE. %ISOPEN Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor automatically after executing its associated SQL statement. %ROWCOUNT Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement, or returned by a SELECT INTO statement.

PL/SQL CURSORS Table s(slno,name) declare total_rows int; begin update s set slno= slno+1; if sql %notfound then dbms_output.put_line('table not found'); elsif sql%found then total_rows:=sql%rowcount; dbms_output.put_line(total_rows||' rows changed in s'); end if; end; Output 2 rows changed in s slno name 1 aji 2 saji slno name 2 aji 3 saji

PL/SQL CURSORS Explicit Cursors Explicit cursors are programmer defined cursors for gaining more control over the context area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement which returns more than one row.

PL/SQL CURSORS The syntax for creating an explicit cursor is CURSOR cursor_name IS select_statement; Explicit cursor involves four steps: Declaring the cursor Opening the cursor Fetching the cursor for retrieving data Closing the cursor

PL/SQL CURSORS Declaring the Cursor Declaring the cursor defines the cursor with a name and the associated SELECT statement. For example: CURSOR c_customers IS SELECT id, name, address FROM customers; Opening the Cursor Opening the cursor allocates memory for the cursor and makes it ready for fetching the rows returned by the SQL statement into it. For example, OPEN c_customers;

PL/SQL CURSORS Fetching the Cursor Fetching the cursor involves accessing one row at a time. For example FETCH c_customers INTO c_id, c_name, c_addr; Closing the Cursor Closing the cursor means releasing the allocated memory. CLOSE c_customers;

PL/SQL CURSORS declare Table S s1 s.slno%type; s2 s.name%type; cursor ss is select * from s; begin open ss; loop fetch ss into s1,s2; exit when ss%notfound; dbms_output.put_line(s1||' '||s2); end loop; close ss; end; Output 1 aji 2 saji slno name 1 aji 2 saji

PL/SQL CURSORS declare srow s%rowtype; a int:=&a; cursor ss is select * from s where slno=a; begin open ss; loop fetch ss into srow.slno,srow.name; exit when ss%notfound; dbms_output.put_line(srow.slno||' '||srow.name); end loop; close ss; end; Output Enter the value of a 2 2 saji

PL/SQL CURSORS Table s(slno,name) & s1(slno,mark) Enter rollno print slno and name Insert slno and mark in the table s1 declare srow s%rowtype; roll int:=&roll; mark int; cursor ss is select * from s where slno=roll; begin open ss; loop fetch ss into srow.slno,srow.name; exit when ss%notfound; dbms_output.put_line(srow.slno||' '||srow.name); mark:=&mark; insert into s1 values(srow.slno,mark); end loop; close ss; end;

PL/SQL CURSORS Table nos(slno,name) even(slno,name) odd(slno,name) Read each records from nos, if slno is even then insert the record in even table Other wise insert the record in odd table declare rows nos%rowtype; cursor c is select * from nos; begin open c; loop fetch c into rows.slno,rows.name; exit when c%notfound; if rows.slno mod 2=0 then insert into even values(rows.slno,rows.name); else insert into odd values(rows.slno,rows.name); end if; end loop; close c; end;

PL/SQL Trigger Oracle allows to define procedures that are implicitly executed when an INSERT, UPDATE, or DELETE statement is issued against a table. These procedures are called database triggers. Triggers are stored in the database separately from their associated tables.

PL/SQL Trigger Triggers are stored programs, which are automatically executed when some events occur. Triggers are, written to be executed in response to any of the following events: A DML statement (DELETE, INSERT, or UPDATE). A DDL statement (CREATE, ALTER, or DROP). A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).

PL/SQL Trigger Syntax for creating a trigger: CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) DECLARE Declaration-statements BEGIN Executable-statements END;

PL/SQL Trigger CREATE [OR REPLACE] TRIGGER trigger_name: Creates or replaces an existing trigger with the trigger_name. {BEFORE | AFTER | INSTEAD OF}: This specifies when the trigger would be executed. {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation. [OF col_name]: This specifies the column name that would be updated. [ON table_name]: This specifies the name of the table associated with the trigger.

PL/SQL Trigger [REFERENCING OLD AS o NEW AS n]: This allows to refer new and old values for various DML statements, like INSERT, UPDATE, and DELETE. [FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be executed for each row being affected. Otherwise the trigger will execute just once when the SQL statement is executed, which is called a table level trigger. WHEN (condition): This provides a condition for rows for which the trigger would fire. This clause is valid only for row level triggers.

PL/SQL Trigger Three parts: Event (activates the trigger) Condition (tests whether the triggers should run) [Optional] Action (what happens if the trigger runs) Events could be : BEFORE|AFTER INSERT|UPDATE|DELETE ON <tableName> e.g.: BEFORE INSERT ON Professor Condition is SQL expression or an SQL query Action can be many different choices : SQL statements ,and even DDL , and “commit”…

PL/SQL Trigger Table emp(empid,empname,dept,salary) high_salary(empid,empname) When insert data in emp table, salary >5000 then insert empid & empname into high_salary table create or replace trigger t1 before insert or update of salary on emp for each row begin if :new.salary >5000 then insert into high_salary values(:new.empid,:new.empname); end if; end;

PL/SQL Trigger Table emp(empid,empname,dept,salary) high_salary(empid,empname) When delete row from emp, it display the row details create or replace trigger t2 after delete on emp for each row Begin dbms_output.put_line(:old.empname||' deleted'); end;

PL/SQL Trigger & procedure Table emp(empid,empname,dept,salary) high_salary(empid,empname) Deleted row will be insert into high_salary, using a procedure create or replace procedure display(roll in int,name in varchar2) as begin dbms_output.put_line(roll||' details deleted'); insert into high_salary values (roll,name); end; create or replace trigger tt after delete on emp for each row display(:old.empid,:old.empname);

PL/SQL Trigger (OLD & NEW) Table emp(empid,empname,dept,salary) When insert or update display salary difference create or replace trigger tri before insert or update on emp for each row when (new.empid >0) declare diff number(10,2); Begin diff:=:new.salary-:old.salary; dbms_output.put_line('OLD SALARY ' ||:old.salary); dbms_output.put_line('NEW SALARY ' ||:new.salary); dbms_output.put_line('SALARY DIFFERENCE ' || diff); end;

PL/SQL Trigger (OLD & NEW) When insert new row, then there is no old value SQL> insert into emp values(100,'popo','mca',22); OLD SALARY NEW SALARY 22 SALARY DIFFERENCE When update then there is old & new value SQL> update emp set salary=salary+500 where empid=100; OLD SALARY 22 NEW SALARY 522 SALARY DIFFERENCE 500

PL/SQL Trigger

PL/SQL Trigger