What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.

Slides:



Advertisements
Similar presentations
Oracle PL/SQL IV Exceptions Packages.
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.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
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.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
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 © SUPINFO. All rights reserved Procedures and functions.
Stored Procedures Functions Packages
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
PL/SQL CSE2132 Database Systems Week 9 Lecture PL/SQL and Programming in Oracle - 2.
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.
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.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
CPS120: Introduction to Computer Science Functions.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
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,
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]
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
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.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Creating Functions. Overview of Stored Functions A function is a named PL/SQL block that returns a value. A function is a named PL/SQL block that returns.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
SQL Triggers, Functions & Stored Procedures Programming Operations.
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.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Creating Stored Functions
Creating Stored Procedures and Functions
PL/SQL.
Creating Packages.
UNIT - V STORED PROCEDURE.
Introduction to PL/SQL
SQL PL/SQL Presented by: Dr. Samir Tartir
PL/SQL Scripting in Oracle:
Oracle Stored Procedures and Functions
Working with Composite Datatypes
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Procedures Oracle & MySQL
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical units. PL/SQL has two types of subprograms called procedures and functions. Generally, you use a procedure to perform an action and a function to compute a value. Like PL/SQL blocks, subprograms have a declarative part, an executable part, and an optional exception-handling part. 1. The declarative part contains declarations of types, cursors, constants, variables and nested subprograms. 2. The executable part contains statements that assign values, control execution, and manipulate Oracle data. 3. The exception-handling part contains exception handlers, which deal with exceptions raised during execution.

Advantages Of sub programs 1.Extensibility:- Allows creation of new program module without affecting existing program module. 2.Modularity: Subprograms also provide modularity; that is, they let you break a program down into manageable, well-defined modules. This supports top-down design and the stepwise refinement approach to problem solving. 3.Reusability & Maintainability: In addition, subprograms promote reusability and maintainability. Once validated, a subprogram can be used with confidence in any number of applications. If its definition changes, only the subprogram is affected. This simplifies maintenance. 4.Abstraction: To use subprograms, you must know what they do, not how they work. Therefore, you can design applications from the top down without worrying about implementation details.

Types Of Subprogram: 1.Local Subprogram: 2.Stored Subprogram: ProcedureFunction 1. A procedure is a subprogram that can accept parameters, perform an action and return parameters. A Function is a subprogram that can accept parameters, computes a value and return that value to caller A procedure may return no value A function must return only one value. Generally we use a procedure to perform an action We use a function to computes a value

Procedure: A procedure is a subprogram that perform a specific action. Procedure can be created with in pl/sql module. Local procedure Stored Procedure Local Procedure : Such procedure are local to the pl/sql module, which contain it. Procedures can be created in the declarative section of pl/sql module, local to the module. The local module can be called anywhere in the module execution section. SYNTAX Declare procedure name[(argument {in,out,in out})] {is|as} [ local declaration] Begin pl/sql subprogram body; end [name]; Begin executable code End;

INOUTIN OUT It is defaultIt must be specified It passes values to a subprogram It returns value to the caller It passes initial values to a sub program and returns updated values to the caller

Consider a procedure that accepts two numbers and return add, sub in other words a procedure to return multiple values through arguments. declare A number; b Number; C number; D number; E number; F number; PROCEDURE process (a in number, b in number, c out number, d out number, e out number, f out number) is Begin C:=a+b;

D:=a-b; E:=a*b; F:=a/b; End process; Begin A:=&firstnumber; B:=&secondnumber; Process(a,b,c,d,e,f,); Dbms_output.put_line(‘Addition is’||c); End;

Consider Declare empno number; Amount number; Procedure raise_salary( emp_id number, increase number) is Current_salary number(3,2); Begin select sal into current_salary from emp where empno=emp_id; Update emp set sal=sal+increase where empno=emp_id; End raise_salary; Begin Empno:=&empno; Amount:=&amount; Raise_salary(empno,amount); End;

Stored Procedure: Create or replace PROCEDURE process (a in number, b in number, c out number, d out number, e out number, f out number) is Begin C:=a+b; D:=a-b; E:=a*b; F:=a/b; End;

Stored Procedure: A pl/sql code to call the procedure process created above. Declare a number; b Number; C number; dnumber; e number; f number; Begin A:=&firstnumber; B:=&secondnumber; Process(a,b,c,d,e,f,); Dbms_output.put_line(‘Addition is’||c); Dbms_output.put_line(‘subtraction is’||d); Dbms_output.put_line(‘Multiplication is’||e); Dbms_output.put_line(‘Divison is’||f); End;

Stored Procedure: A stored procedure fire_employee to delete employee on the basis of employee number Sol Create or replace procedure fire_employee (emp_id in number) is Begin delete from emp where empno=emp_id; End;

Stored Procedure: Pl/SQl code to call the procedure fire_employee created in above slide. Sol Declare e number; Begin e:=&empno; fire_employee(e); End;

Functions A function is a subprogram that computes a value. Functions and procedures are structured alike, except that functions have a RETURN statement. There are two types of functions: 1.Local functions 2.Stored Functions Syntax To define a Function Local to block:--- Declare function function_name[(argument {in,out,in out})] return datatype {is|as} [ local declaration] Begin pl/sql subprogram body; end [name]; Begin executable code End;

Functions RETURN STATEMENTS:- The return statement immediately completes the execution of a subprogram and returns control to the caller. Execution then resume with the statement following the subprogram call. In procedure, a return statement cannot contain an expression. The stmnt simply returns control to the caller before the normal end of the procedure is reached. In Function, A return stmnt must contain an expression, which is evaluated when the return stmnt executed. The function must contain atleast one RETURN stmnt.

Functions Write a pl/sql code that calls a function to add two numbers. Sol:-- Declare a number; b number; c number; Function addn (a in number, b in number) Return number is Begin c:=a+b; return(c); end addn; Begin a:=&first_number; b:=&second_number; c:=addn(a,b); dbms_output.put_line(‘addition is’||c); End;

Functions Write a pl/sql code that calls a function balance to return the balance of a specified bank accounnt. Sol:-- Declare acc_no number; bal number; acct_bal number; function balance(acct_id number) return number is Begin select bal into acct_bal from accts where acct_no=acct_id; return(acct_bal); end balance; Begin acc_no:=&acc_no; bal:=balance(acc_no); dbms_output.put_line(‘balance is’||bal); End;

Functions Write a pl/sql code that to show that a function can use both the out parameter and return statement to return the multiple values to tha calling subprogram. Sol:-- Declare a number; b Number; C number; dnumber; function addn(a in number,b in number,c out number) return number is Begin c:=a+b; d:=a-b; return(d); end addn; Begin a:=&first_number; b:=&second_number; d:=addn(a,b,c); dbms_output.put_line(‘addition is’||c); dbms_output.put_line(‘subtraction is’||d); End;

Functions Syntax for stored Procedure :- Create or replace function function_name[(argument {in,out,in out})] return datatype {is|as} [ local declaration] Begin pl/sql subprogram body; End;

Functions Create a stored function that acce[pts two numbers and return addition of passed values. Sol:- Create or replace Function addn (a in number, b in number) Return number is c number; Begin c:=a+b; return(c); end addn;

Functions A pl/sql code to call the function addn created in above slide.. Sol:- Declare a number; b number; c number; Begin a:=&first_number; b:=&second_number; c:=addn(a,b); dbms_output.put_line(‘addition is’||c); End;

Functions A stored procedure thatb accepts department number and return total salary of that department. Sol:- Create or replace Function salary(dept number)Return number is s number; Begin select sum(sal) into s from emp where deptno=dept; return(s); end addn;

Functions A pl/sql code to call the function salary created in above slide.. Sol:- Declare d number; Begin d:=&dept_no; sal:=salary(d); dbms_output.put_line(‘salary of deptno’||d||’is’||sal); End;

PACKAGE Packages are group of procedures, functions, variables, constants, cursors and SQL statements grouped together into a single unit. The tool used to create a package is SQL* Plus. A package once written and debugged is compiled and stored in oracle’s system tables held in an oracle database. Component of an Oracle Package:- A package has two components: 1.Package specification 2.Package Body Package Specification:- A package’s specification declares the memory variables, constants, cursors and subprograms that are available for use. Syantax:- Create or replace package package_name as Function function_name(list of arguments) return datatype; Procedure procedure_name(list of arguments); End package_name;

PACKAGE package Body:- The body of a package contains the definition of public objects that are declared in the specification. In implements the specifications. Unlike package specification, the package body can contain subprogram bodies. SYNTAX:- Create or replace package body package_name as Function function_name(list of arguments) return datatype{is|as} [local declaration of function] Begin --code of function; End function_name; Procedure procedure_name(list of arguments){is|as} [local declaration of procedure] Begin --code of procedure; End procedure_name; End package_name;

PACKAGE:- Referencing Package Contents: The package contents can be referenced from database triggers, stored subprograms or pl/sql blocks. To reference a package’s subprogram and objects we must use dot notations. Syntax for dot notation: Package_name.type_name Package_name.object_name Package_name.subprogram_name In this Syntax: Package_name: is the name of declared package type_name: is the name of the type that is user defined, such as record. object_name : is the name of the constant or variable that is declared by the users. subprogram_name: is the name of the procedure or function contained in the package body. For eg: To reference a procedure addn in package bank we can use: Bank.addn(list_of_arguments)

PACKAGE Advantages of packages:-  Modularity:- packages encapsulate related types, objects, procedures, functions, variables, constants, cursors together in a named pl/sql module.  Easier Application Design:- A package specification can be coded and compiled without its body which increases the designer productivity.  Information Hiding:- By hiding implementation details in package body, better security is provided to the system.  Added Functionality:- A package’s public variable and cursor persist for the duration of the session. Therefore all cursors and procedures that execute in this environment can share them.  Better performance:- packages improve performance by loading multiple objects in memory at once. Therefore subsequent calls to related subprograms in the package require no input/output

PACKAGE Create a package specification for the insert_oper,retrieve,update_oper and delete_oper whose description is giver below: Sol:- Create or replace package operation as procedure insert_oper(eno number,name varchar,job varchar,sal number, dno number); procedure retrieve(eno in number, name out varchar, sal out number); function update_oper(dno number,amount number) return number; function delete_oper(eno number) return char; End operation;

PACKAGE Create a package body for the insert_oper,retrieve,update_oper and delete_oper as described above Sol:- Create or replace package body operation as procedure insert_oper(eno number,name varchar,job varchar,sal number, dno number) is begin insert into emp (empno,ename,job,sal,deptno) Values (eno,name,job,sal,dno); end insert_oper; procedure retrieve (eno in number,name out varchar,sal out number) is begin select ename,sal into name, sal from emp where empno=eno; end retrieve;

function update_oper(dno number,amount number) return number is N number; Begin update emp set sal=sal+amount where deptno=dno; n:=sql%rowcount; return(n); End update_oper; function delete_oper(eno number) return char is begin delete emp where empno=eno; if sql%found then return(‘y’); else RETURN(‘N’); END IF; end delete_oper; End operation;

PACKAGE:- Write a pl/sql code to call insert_oper procedure to ionsert a record in emp table. Sol:- Declare eno number; Name varchar(20); Job varchar(20); Sal number; Dno number; Begin Eno:=&employee_number; Name:=&employee_name; Job:=&employee_job; Sal:=&employee_salary; Dno:=&employee_deptno; Operation.insert_oper(eno,name,job,sal,dno); End;