PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.

Slides:



Advertisements
Similar presentations
Owners Roles DB Objects Grant Revoke Grant Revoke Grant Revoke (*) with admin option Il ruolo …. È un set di system & object privileges Non appartiene.
Advertisements

8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
PL/SQL.
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.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
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
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.
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)
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Distributed Database Applications COSC 5050 Week Three.
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
PL/SQL CSE2132 Database Systems Week 9 Lecture PL/SQL and Programming in Oracle - 2.
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.
Subqueries.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
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.
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.
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 Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
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)
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.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Chapter Sixteen Cursors Objective: – Introduction to cursors – Use of cursors in a database record – Implicit & explicit cursors – Cursors & loops – Cursors.
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.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
implicit and an explicit cursor
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
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.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
PL/SQL CURSORS.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Interacting with the Oracle Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Cursors ITEC 224 Database Programming PL/SQL Lab.
SQL PL/SQL Presented by: Dr. Samir Tartir
Chapter Eighteen Cursors
Database Management Systems 2
Interacting with the Oracle Server
Database Management Systems 2
Database Management Systems 2
Working with Composite Datatypes
Introduction to PL/SQL Procedures & Cursors
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Cursors.
Chapter 03 DBMS.
Chapter 8 Advanced SQL Pearson Education © 2009.
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

PL/SQL Cursors Session - II

Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen

%TYPE%TYPE useful when declaring variables that refers to Database Columns Name Varchar(15); Name Emp.Ename%TYPE;

%ROWTYPE%ROWTYPE Provides a record Type that represents a Row in a Table. ENAMEDESIGNATION SAL SMITH JONES ADAMS KING CLERK SALESMAN PRSIDENT MANAGER

Name Varchar(15); Desig Varchar(15); Salary Number(8,2); Emp_Rec Emp%ROWTYPE;

Declare EmRec Emp%Rowtype; Begin Select * INTO EmRec from emp where empno=7369; Dbms_Output.put_line( emrec.ename||emrec.Sal); End;Declare EmRec Emp%Rowtype; Begin Select * INTO EmRec from emp where empno=7369; Dbms_Output.put_line( emrec.ename||emrec.Sal); End;

CURSORSCURSORS It is a Temporary Table created in the SGA of the Server. It is a Temporary Table created in the SGA of the Server.

Two Types of CURSORS l IMPLICIT l EXPLICIT

IMPLICIT Cursor PL/SQL Implicitly declares a cursor for all SQL data Manipulation Statements, Including Queries that return only One Row

EXPLICIT Cursor Declares a cursor that process more than one row individually.

Using Explicit Cursors l Declare the Cursor l Open theCursor l Fetch theCursor l Close theCursor l Declare the Cursor l Open theCursor l Fetch theCursor l Close theCursor

Declaring a Explicit Cursor While Declaring we have to name it and Associate with a Query. Declare CURSOR emp_cursor IS SELECT ename,deptno from emp where sal>2000; Begin

Opening a Explicit Cursor Opening the Cursor executes the SELECT statement and Identifies the active set. OPEN emp_curs; Opening the Cursor executes the SELECT statement and Identifies the active set. OPEN emp_curs;

Fetching a Explicit Cursor Fetch statement retrieves each row in the active set, one at a time. FETCH emp_cur INTO emp_name; Fetch statement retrieves each row in the active set, one at a time. FETCH emp_cur INTO emp_name;

Closing a Explicit Cursor Close Statement disables the Cursor. CLOSE emp_curs; Close Statement disables the Cursor. CLOSE emp_curs;

C DEFINE CURSOR ENAME DEPTNO SMITH JONES ADAMS CURSOR C is Select Ename,deptno from emp ; OPEN CURSOR OPEN C;

ENAME DEPTNO FETCHING CURSOR SMITH JONES ADAMS FETCH C INTO ENAM,DEPTN; CLOSING CURSOR CLOSE C;

Declare enam varchar(15); depno number(3); CURSOR C Is Select ename,deptno from emp where job=‘SALESMAN’; Begin OPEN C; LOOP FETCH C into enam,depno ; exit when c%notfound; dbms_output.put_line(enam||depno); END LOOP; Close C; End; Declare enam varchar(15); depno number(3); CURSOR C Is Select ename,deptno from emp where job=‘SALESMAN’; Begin OPEN C; LOOP FETCH C into enam,depno ; exit when c%notfound; dbms_output.put_line(enam||depno); END LOOP; Close C; End;

Explicit Cursor Attributes l Every Explicit cursor has four attributes. l Lets you access usefull information about the execution of a multirow query. l Every Explicit cursor has four attributes. l Lets you access usefull information about the execution of a multirow query.

l %NOTFOUND l %FOUND l %ROWCOUNT l %ISOPEN l %NOTFOUND l %FOUND l %ROWCOUNT l %ISOPEN

%NOTFOUND%NOTFOUND Evaluates to TRUE if the last FETCH failed because no more rows were available. Loop Fetch emp_cur INTO enam,depno; EXIT WHEN emp_cur %NOTFOUND; End loop; Evaluates to TRUE if the last FETCH failed because no more rows were available. Loop Fetch emp_cur INTO enam,depno; EXIT WHEN emp_cur %NOTFOUND; End loop;

%FOUND%FOUND l Is the Logical Opposite of %NOTFOUND Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%FOUND Then Dbms_output.Put_line(‘Record Found’); else exit; End if; End loop; l Is the Logical Opposite of %NOTFOUND Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%FOUND Then Dbms_output.Put_line(‘Record Found’); else exit; End if; End loop;

%ROWCOUNT%ROWCOUNT Returns the number of rows Fetched

n:=&number; open emp_cur; loop Fetch emp_cur INTO enam,deptn; IF emp_cur%ROWCOUNT>n Then Dbms_output.Put_line('more than '||n||' records'); exit; End if; exit when emp_cur%notfound; End loop; Close emp_cur; n:=&number; open emp_cur; loop Fetch emp_cur INTO enam,deptn; IF emp_cur%ROWCOUNT>n Then Dbms_output.Put_line('more than '||n||' records'); exit; End if; exit when emp_cur%notfound; End loop; Close emp_cur;

%ISOPEN%ISOPEN Checks Cursor is Opened Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%ISOPEN Then Dbms_output.Put_line(‘Opened’); End if; End loop; Checks Cursor is Opened Loop Fetch emp_cur INTO enam,detpn; IF emp_cur%ISOPEN Then Dbms_output.Put_line(‘Opened’); End if; End loop;

Implicit Cursor Attributes l Use cursor attributes to access the SQL% cursors context area. l %NOTFOUND, %FOUND l %ROWCOUNT

%NOTFOUND%NOTFOUND Evaluates True if any INSERT, UPDATE or DELETE affected no Rows. Update emp set ename=‘RAM’ where empno=3445; IF SQL%NOTFOUND then Evaluates True if any INSERT, UPDATE or DELETE affected no Rows. Update emp set ename=‘RAM’ where empno=3445; IF SQL%NOTFOUND then

Cursor For Loops It Implicitly OPENS a Cursor, FETCH each row returned by the query associated with Cursor and CLOSE the Cursor. Advantages :- Lesser Coding It Implicitly OPENS a Cursor, FETCH each row returned by the query associated with Cursor and CLOSE the Cursor. Advantages :- Lesser Coding

declare cursor lst is select * from emp; begin for I in lst loop dbms_output.put_line( I.ename|| I.job); end loop; end; declare cursor lst is select * from emp; begin for I in lst loop dbms_output.put_line( I.ename|| I.job); end loop; end;

Dynamic Cursors

DECLARE CURSOR c1 (dnum number) IS Select * from emp where deptno = dnum; dep emp.deptno%type; BEGIN Select deptno into dep from emp where ename ='SMITH'; FOR emp_rec IN c1(dep) loop Dbms_output.put_line(emp_rec.ename) ; End loop; END;