Database Programming Using Oracle 11g

Slides:



Advertisements
Similar presentations
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Advertisements

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.
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
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
PL/SQL Introduction 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.
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.
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.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
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.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
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.
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.
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.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Stacked Canvas A content canvas is a main from canvas while a stacked canvas is a secondary canvas that overlays or partially covers a content canvas.
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.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
implicit and an explicit cursor
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
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.
Composite data types_ PL/SQL table. Composite data types PL/SQL provides three composite data types : TABLE, ROCORD, and VARRAY. Objects of type TABLE.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Writing Executable Statements
PL/SQL MULTIPLE CHOICE QUESTION.
Writing Control Structures
SQL PL/SQL Presented by: Dr. Samir Tartir
Interacting with the Oracle Server
Database Management Systems 2
كتابة الجمل التنفيذية في PL/SQL
Writing Correlated Subqueries
Part B Entity Relationship Diagram TITLE #* ID * title * description
Working with Composite Datatypes
Cursors.
Chapter 03 DBMS.
PL/SQL week10.
PL/SQL Declaring Variables.
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Database Programming Using Oracle 11g Records

Records Record Composite Datatype Can hold more than one piece of information as compare to Scalar Datatype like number , varchars Single variable name can refer to row of a table

Records Type of Record Table Base Cursor Base User Defined

Records Type of Record Records can refer to all columns of the database table. RowType in used in Oracle to create table-based records

Records Implementing Table-based Records - I

Records declare emp_rec emp%rowtype; begin select * into emp_rec from emp where empno=7369; dbms_output.put_line (emp_rec.ename || ' ' || emp_rec.job); exception when no_data_found then dbms_output.put_line('No matching record found'); end;

Records Implementing Table-based Records - II

Records DECLARE emp_rec emp%ROWTYPE; BEGIN emp_rec.empno := 500; emp_rec.ename := 'Special'; emp_rec.ename := 'Consultant'; emp_rec.mgr := 7369; emp_rec.hiredate := sysdate; emp_rec.sal := 2000; emp_rec.comm := NULL; emp_rec.deptno := 10;

Records INSERT INTO emp VALUES emp_rec; dbms_output.put_line('Record base insertion is done'); END;

Records Cursor-based Records It can refer to all columns of the cursor defined . RowType in used in Oracle to create Cursor based records

Records Implementing Cursor -based Records - I

Records Write a PL/SQL block which should look for first occurrence of employee either with designation of CLERK or salary greater than 3000. Program should exit after this displaying success message

Records declare cursor c1 is select * from emp; c2 c1%rowtype; begin open c1; loop fetch c1 into c2; if c2.sal > 2000 or c2.job='CLERK' then dbms_output.put_line ('Value is matched'); exit end if; exit when c1%notfound; end loop; end;

Records Implementing Cursor -based Records - II

Records declare cursor c1 is select empno,ename,dname from emp e, dept d where e.deptno=d.deptno; c2 c1%rowtype; begin open c1; loop fetch c1 into c2; if c2.dname='SALES' then update emp set sal = sal*0.20 + sal where empno=c2.empno;

Records dbms_output.put_line ('Salary is updated by 20% of Employee no: ' || c2.empno); end if; exit when c1%notfound; end loop; end;

Records User Defined Record (UDR) UDR is composite datatype With UDR programmer have control over defination Define structure of UDR Define variable of UDR type

Records Syntax of User Defined Record

Records Step# 1: TYPE <type_name> IS RECORD (<field_name1> <datatype1>, <field_name2> <datatype2>, ... <field_nameN> <datatypeN> ); Step # 2: Declare variable of Type_name;

Records Syntax of User Defined Record (UDR) With UDR programmer is able to define its own structure Multiple variables of same UDR can be created

Records Implementing User Defined Record - I

Records Write a PL/SQL block to display the information of first 5 employees who are working as MAN in their ename

Records declare type emp_rec is RECORD (empno emp.empno%type, ename emp.ename%type, sal emp.ename%type); emp_rec_val emp_rec; cursor c1 is select empno, ename, sal from emp where job like ('%MAN%'); begin open c1;

Records loop fetch c1 into emp_rec_val; dbms_output.put_line (emp_rec_val.ename || emp_rec_val.sal ); exit when c1%rowcount > 5; end loop; end;

Records Implementing User Defined Record - II

Records declare type emp_rec is RECORD (empno emp.empno%type, ename emp.ename%type, sal emp.ename%type); emp_rec_val emp_rec; cursor c1 is select empno, ename, sal from emp where job like ('%MAN%'); begin open c1;

Records loop fetch c1 into emp_rec_val.empno, emp_rec_val.ename,emp_rec_val.sal; if emp_rec_val.ename ='BLAKE' then insert into history values (emp_rec_val.empno, emp_rec_val.ename,sysdate()); dbms_output.put_line ('Data Added in history table' ); end if ; exit when c1%rowcount > 5; end loop; end;