Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask PL-SQL Expert

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.

PL-SQL, Programming

  • Category:- PL-SQL
  • Reference No.:- M9510472

Have any Question?


Related Questions in PL-SQL

For this assignment you will be provided a database backup

For this assignment, you will be provided a database backup for a database called FinanceDB. You will have to restore this backup to your own version of SQL Server. All of the questions in this assignment relate to the F ...

Complete the following tasksin microsoft access create the

Complete the following tasks: In Microsoft Access, create the database and tables that you identified in W3 Assignment 2. In Microsoft Word, write the SQL statements to create the database and tables. Write SQL statement ...

Continuing the project you have worked on in weeks 1-4 in

Continuing the project you have worked on in Weeks 1-4, in this final week, complete the following tasks: Refine your database and SQL statements by incorporating your instructor's feedback. Verify that the database comp ...

Assignment - queries functions and triggersaimthe aims of

Assignment - Queries, Functions and Triggers Aim The aims of this assignment are to: formulate SQL queries; populate an RDBMS with a real dataset, and analyse the data; design test data for testing SQL queries; create SQ ...

Purpose of the assessment with ulo mapping the purpose of

Purpose of the assessment (with ULO Mapping) The purpose of this assignment is to develop skills in managing data in databases and to gain understanding of data model development and implementation using a commercially a ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As