Functions in pl/sql, PL-SQL Programming

Functions 

The function is a subprogram that calculates a value. The Functions and procedures are structured similar, except that the functions have a RETURN clause. You can write functions using the syntax as shown below:

FUNCTION name [(parameter[, parameter, ...])] RETURN datatype IS

[local declarations]

BEGIN

executable statements

[EXCEPTION

exception handlers]

END [name];

Where the parameter stand for the following syntax which is as shown below:

parameter_name [IN | OUT [NOCOPY] | IN OUT [NOCOPY]] datatype_name

[{:= | DEFAULT} expression]

The datatype of a parameter or function result cannot be constrained. Though, you can use a workaround to size-constrain them indirectly.

Similar to a procedure, a function has 2 parts: the specifications & the body. The function specification starts with the keyword FUNCTION and ends with the RETURN clause that specifies the datatype of the result value. The Parameter declarations are not obligatory.

The Functions that take no parameters are written without the parentheses. The function body starts with the keyword IS and ends with the keyword END followed by an optional function name. The function body has 3 parts: a declarative section, an executable section, & an optional exception-handling section.

The declarative section contains the local declarations, that are placed between the keywords IS & BEGIN. The keywords DECLARE is never used. The executable section contains statements that are placed between the keywords BEGIN and EXCEPTION (or END). One or more RETURN statements should appear in the executable section of a function. The exception-handling section contains exception handlers that are placed between the keywords EXCEPTION and END. Consider the function sal_ok, that determines if an employee salary is out of range:

FUNCTION sal_ok (salary REAL, title REAL) RETURN BOOLEAN IS

min_sal REAL;

max_sal REAL;

BEGIN

SELECT losal, hisal INTO min_sal, max_sal

FROM sals

WHERE job = title;

RETURN (salary >= min_sal) AND (salary <= max_sal);

END sal_ok;

Whenever called, this function accepts an employee salary and the job title. It uses the job title to select the range limits from the sals database table. The sal_ok, function identifier, is set to a Boolean value by the RETURN statement. When the salary is out of range, the sal_ok is set to FALSE; or else, the sal_ok is set to TRUE.

The function is called as the part of an expression. For illustration, the function sal_ok might be called as shown:

DECLARE

new_sal REAL;

new_title VARCHAR2(15);

BEGIN

...

IF sal_ok(new_sal, new_title) THEN ...

The function identifier sal_ok acts such as a variable whose value depends on the parameters passed to it.

Posted Date: 10/5/2012 5:24:10 AM | Location : United States







Related Discussions:- Functions in pl/sql, Assignment Help, Ask Question on Functions in pl/sql, Get Answer, Expert's Help, Functions in pl/sql Discussions

Write discussion on Functions in pl/sql
Your posts are moderated
Related Questions
THEO R Y OF SPECIAL CREATION - Life originated on the earth due to natural events by the super natural power. The biblical story of creation of world within six days was p

SQLs counterpart of the key words: The text from the opening parenthesis to the end of the fourth line specifies the declared type of the table, meaning that every table ever

Cursor Variables Similar to a cursor, cursor variable points to the current row in the result set of a multi-row query. But, dissimilar a cursor, a cursor variable can be opene

Pass the nulls to a dynamic SQL: Passing Nulls: Assume that you want to pass the nulls to a dynamic SQL statement. For illustration, you may write the EXECUTE IMMEDIATE

This task involves developing some functions that extract data from an SQL database. The scenario is that a company which owns an online vehicle search website wants to generate so

Operators on Tables and Rows Row Extraction TUPLE FROM r, SQL has row subqueries. These are just like scalar subqueries except that they may specify more than one column.

Data Types in SQL - Integer INTEGER or  synonymously INT, for integers within a certain range. SQL additionally has types SMALLINT and BIGINT for certain ranges of integers. T

Using a join on 3 tables, select 5 columns and 10 rows from the 3 tables without the use of a Cartesian product Query: SELECT E.LAST_NAME, E.FIRST_NAME, S.BUILDING, S.BRAN

Write a program to implement the inverted file shown in the slides (Simple Index file, LabelID file and Data file).  Use the Avail_List to point at the deleted Label IDs so that th

THEO R Y OF ETERNITY OF LIFE (PRAYER - 1880) - The theory of eternity of life, also called the steady-state theory , states that life has ever been in existence as at presen