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;