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

Built-in functions-comparison operators, Built-In Functions The PL/SQL p...

Built-In Functions The PL/SQL provides a lot of powerful functions to help you to manipulate the data. These built-in functions fall into the categories as shown below: error r

Using invoker rights in pl sql, Using Invoker Rights: By default, the ...

Using Invoker Rights: By default, the stored procedure executes with the privileges of its definer, not its invoker. These procedures are bound to the schema in which they inh

Fetching from a cursor variable, Fetching from a Cursor Variable The F...

Fetching from a Cursor Variable The FETCH statement retrieve rows one at a time from the product set of a multi-row query. The syntax for the same is as shown: FETCH {curso

Varrays versus nested tables, Varrays versus Nested Tables The Nested ta...

Varrays versus Nested Tables The Nested tables are differing from varrays in the following ways: 1)  Varrays have a maximum size, while nested tables do not. 2)  Varrays are

Rowid - sql pseudocolumns, ROWID The ROWID returns the rowid (binary a...

ROWID The ROWID returns the rowid (binary address) of a row in the database table. You can use the variables of the type UROWID to store rowids in a readable format. In the il

I need simple hosted sql database, Project Description: I want a small r...

Project Description: I want a small relational database to be built. I want the database to have the subsequent information tables: Employee Information Document storage

Oracle development , Write a stored procedure that accepts the post code in...

Write a stored procedure that accepts the post code in which the customer resides as the input parameter. The procedure should then use an explicit cursor to display comprehensive

I want online credit application website, Project Description: We organi...

Project Description: We organize an online system called ACPAS we have created a project called EVO that can be use by our customers to integrate their web sites with the Acpas

Effects of null for table expression, Effects of NULL for Table Expression ...

Effects of NULL for Table Expression Here's an important distinction between expressions denoting tables and expressions denoting multisets of rows: a table expression cannot

Product-specific packages in pl/sql, Product-specific Packages The Ora...

Product-specific Packages The Oracle and different Oracle tools are supplied with the product-specific packages which help you to build the PL/SQL-based applications. For illu

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