Functions in pl/sql, PL-SQL Programming

Assignment Help:

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.


Related Discussions:- Functions in pl/sql

Cursor variables, What Are Cursor Variables  ? The Cursor variables ar...

What Are Cursor Variables  ? The Cursor variables are like C or Pascal pointers that hold the memory location (address) of some item rather of the item itself. Therefore, decl

Exceptions - syntax, Exceptions An exception is the runtime error or wa...

Exceptions An exception is the runtime error or warning condition that can be predefined or user-defined. The Predefined exceptions are raised implicitly through runtime system

Create a procedure that update the status, Create a procedure named STATUS_...

Create a procedure named STATUS_SHIP_SP that allows a company to employee in the Shipping Department to update the status of an order to add shipping information. The BB_BASKETSTAT

Semidifference via not in and a subquery , Semidifference via NOT IN and a ...

Semidifference via NOT IN and a subquery SELECT StudentId FROM IS_CALLED WHERE Name = 'Devinder' AND StudentId NOT IN (SELECT StudentId FROM IS_ENROLLED_ON WHER

Anatomy of a table, Anatomy of a Table: Figure shows the terminology u...

Anatomy of a Table: Figure shows the terminology used in SQL to refer to parts of the structure of a table. As you can see, SQL has no official terms for its counterpa

Opening a cursor, Opening a Cursor Opening the cursor executes the que...

Opening a Cursor Opening the cursor executes the query & identifies the result set that consists of all rows that meet the query search criteria. For the cursors declared usin

Miller-urey''s experimental procedure, MILLER-UREY' S EXPERIMENTAL PROCEDU...

MILLER-UREY' S EXPERIMENTAL PROCEDURES - They recreated the probable conditions on the primitive earth in the laboratory. An atmosphere containing hydrogen, ammonia, me

Number types in pl/sql, Number Types The Number types permit you to sto...

Number Types The Number types permit you to store the numeric data (real numbers, integers, and floating-point numbers), show quantities, and do computations. BINARY_INTEG

Magento change address format depending on store, Magento change address fo...

Magento change address format depending on store Project Description: What I need is that depending on the store in which the customer bought the address should change the fo

Types of evolution, TYPES OF EVOLUTION - Sequential evolution         ...

TYPES OF EVOLUTION - Sequential evolution                  :                    Minor changes in the gene pool of a population from one generation to the next, with the resul

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd