Вы находитесь на странице: 1из 7

10/24/2015 Campus Commune

About

×
×

Contact Us

About Certifications

Hello user!

As you have successfully completed the Aspire course curriculum, you can now further your learning
process by signing up for a certification course.

Certifications establish credibility by serving as a proof that you have successfully completed a rigorous
online training and also serve as a token of recognition from well­established institutes. Completing one
or more certifications will give you an additional advantage during the Initial Learning Program.

Basic Certification

1.  Installing, Configuring, and Administering Microsoft Windows 7
2.  Installing, Configuring, and Administering Microsoft Windows 2008 Server
3.  Installing, Configuring, and Administering Microsoft Windows 2012 Server
4.  Red Hat Certified Engineer (RHCE) Certification
5.  Red Hat Certified Virtualization Administrator (RHCVA) Certification
6.  Cisco Certified Network Associate (CCNA) Certification
7.  ITIL 2011 Foundation
8.  Installing, Configuring, and Administering Microsoft SQL Server 2008
9.  Installing, Configuring, and Administering Microsoft SQL Server 2012
10.  Microsoft Certified Technology Specialist (MCTS): Microsoft Exchange Server 2010,
Configuration Certification
11.  MCTS: Administering and Deploying System Center 2012 Configuration Manager Certification

Leaderboards

Close
×

List of Badges

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 1/7
10/24/2015 Campus Commune

Close Genius
Intellect 
Guru
 Earn this! 
Unix/C++ Oracle Lounge
 Earn this!
 Earn this!
Oracle LogoutPre­ILP Home

1.Introduction to Information Management

2.SQL Joins and SubQueries

3.DBObjects & SQL Loader

4.Introduction to PLSQL

5.Named PLSQL Blocks

Q
 Course Completion Quiz
   

5.2. Functions

1.1 Objective
•    Sub program
•    Block Structure
•    Advantages
•    Types
•    Functions
•    Basic Structure
•    Execution

1.2 Course Content

1.2.1 Sub Program
Sub program is a program unit that performs specific task
https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 2/7
10/24/2015 Campus Commune

These can be invoked by another sub program or program , which is called the  calling program and can
be invoked with a set of parameters
These can be created 
•    At Schema Level
•     Inside a Package
•    Inside a PL/SQL Block

A Schema Level sub program is a standalone sub program that is created with CREATE  keyword and
stored in the database and can be dropped using DROP keyword.
A subprogram created inside a package is a packaged subprogram. It is stored in the  database and can
deleted only when the package is deleted.
PL/SQL subprograms are the named PL/SQL blocks that can be invoked with a set of parameters.

1.2.2 Block Structure
<header>
IS | AS
  ­ ­    Declaration Section
BEGIN
  ­ ­    Executable Section
EXCEPTION (Optional)
  ­ ­    Exception section
END;

<header> Specifies PL/SQL subprograms type , Name of a subprogram
Parameter list if exists , RETURN clause depending upon subprogram type
Declarative Part 
•    Optional part.
•    Does not start with DECLARE keyword.
•    Contains declarations of types , cursors , constants, variables, exceptions and nested sub programs.
•    The above declarations are local to the subprogram and cease to exist when the subprogram completes
execution.
 Executable Part
•    Contains statements that assigns values , control execution and manipulation data.
 Exception Handling Part
•    Contains code that handles run time erors.

1.2.3 Advantages

  Easy maintenance
  Improved data security and integrity
  Improved performance
  Improved code clarity

1.2.4 Types
PL/SQL provides two types of sub programs: 

  Procedure
  Function

1.2.5 Functions
Functions are Sub program that always return a value

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 3/7
10/24/2015 Campus Commune

These needs to be declared  and defined before invoking.
These can accept one , many or no parameters
Does not necessarily have parameters , but must have a RETURN value whose data  type is declared in
the header.

1.2.6 Basic Structure

The basic syntax for writing a function is 

CREATE OR REPLACE
FUNCTION FUNCTION <function_name> [ <parameter> [ MODE ] <data type> ]
RETURN return_data type  
IS | AS
[ local variable declaration ]
BEGIN
PL/SQL executable statements
RETURN <v_val>
[ EXCEPTION
Exception handlers]
END [ function_name] ;

•    Function­name specifies the name of the function. 
•    CREATE OR REPLACE option allows create a new function or modifying an existing function. 
•    The optional parameter list contains name, mode and types of the parameters. 
•    The function must contain a return statement. 
•     RETURN clause specifies that data type you are going to return from the function. 
•     The AS keyword is used instead of the IS keyword for creating a standalone function. 
•    Function­body contains the executable part and at the end it contains return statement returning the
value. 

Return Type:

The header section defines the return type of the function. 
The return data type can be any of the oracle data type like VARCHAR, NUMBER etc.

The execution and exception section both should return a value which is of the data type defined in the
header section.
We can use cursors, conditional statements and loops in functions also.
We also can use exception techniques that we learnt in anonymous blocks in functions.

Example1:

CREATE or REPLACE FUNCTION func1(a number) 
RETURN number
AS
    b number;
    c number;
BEGIN
    b:=&num;
    c:=a+b;

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 4/7
10/24/2015 Campus Commune

    RETURN c;
END;

Example2:

CREATE OR REPLACE FUNCTION employer_details_func(p_id number)
RETURN VARCHAR IS 
emp_name VARCHAR(20); 
BEGIN 
SELECT first_name INTO emp_name 
FROM employee WHERE empid = p_id;
RETURN emp_name;
10> END;

1.2.7 Execution
A function can be executed in the following ways.
1) we can invoke the function using blocks , by defining a variable to catch the return value.

DECLARE
employee_name varchr2(30);
v_dept_id  number 
BEGIN
v_dept_id := 20;
employee_name :=  employer_details_func(v_dept_id);
dbms_output.put_line('employee name is ­­>'||employee_name)
END;
/

2) As a part of a SELECT statement

SELECT dept_id,employer_details_func(dept_id) FROM employee where dept_id = 20 ;

3) In a PL/SQL Statements.

dbms_output.put_line(employer_details_func(20));

This line displays the value returned by the function. 

Related Videos

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 5/7
10/24/2015 Campus Commune

Ask a doubt  (Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

   
×

Quiz

Your Dashboard

Bharat Khatwani

 779 Miles

Current Course Progress

100

Overall Progress

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 6/7
10/24/2015 Campus Commune

100

 

https://campuscommune.tcs.com/communities/unix­c­oracle­lounge­0/content/functions­1 7/7

Вам также может понравиться