Parameter default values, PL-SQL Programming

Parameter Default Values

As the illustration below shows, you can initialize the IN parameters to the default values.

In that way, you can pass various numbers of actual parameters to a subprogram, accommodating or overriding the default values as you please. Furthermore, you can add new proper parameters without having to change every call to the subprogram.

PROCEDURE create_dept (

new_dname CHAR DEFAULT 'TEMP',

new_loc CHAR DEFAULT 'TEMP') IS

BEGIN

INSERT INTO dept

VALUES (deptno_seq.NEXTVAL, new_dname, new_loc);

...

END;

If an actual parameter is not passed, then the default value of its corresponding formal

parameter is used. Consider the calls below to create_dept:

create_dept;

create_dept('MARKETING');

create_dept('MARKETING', 'NEW YORK');

The first call passes no actual parameters; therefore both the default values are used. The second call passes only an actual parameter; therefore the default value for new_loc is used.

The third call passes the two actual parameters, so neither of the default value is used. Typically, you can use the positional notation to override the default values of proper parameters. Though, you cannot skip a formal parameter by leaving out its actual parameter. For illustration, the call below incorrectly relates the actual parameter 'NEW YORK' with the formal parameter new_dname:

create_dept('NEW YORK'); -- incorrect

You cannot answer the problem by leaving a placeholder for the actual parameter. For illustration below, the call is illegal:

create_dept(, 'NEW YORK'); -- illegal

In such cases, you must use named notation, which is as shown below:

create_dept(new_loc => 'NEW YORK');

You also not assign a null to an uninitialized formal parameter by leaving out its real parameter. For illustration, given the declaration

DECLARE

FUNCTION gross_pay (

emp_id IN NUMBER,

st_hours IN NUMBER DEFAULT 40,

ot_hours IN NUMBER) RETURN REAL IS

BEGIN

...

END;

The function call below does not assign a null to ot_hours:

IF gross_pay(emp_num) > max_pay THEN ... - illegal

Rather, you should pass the null explicitly, as in

IF gross_pay(emp_num, ot_hour => NULL) > max_pay THEN ...

Or you can initialize ot_hours to NULL, which is as shown:

ot_hours IN NUMBER DEFAULT NULL;

Finally, if creating a stored subprogram, you cannot use the host variables in the DEFAULT clause. The SQL Plus below illustrates causes a bad bind variable error as at the time of creation, num is merely a placeholder whose value may change:

SQL> VARIABLE num NUMBER

SQL> CREATE FUNCTION gross_pay (emp_id IN NUMBER DEFAULT :num, ...

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







Related Discussions:- Parameter default values, Assignment Help, Ask Question on Parameter default values, Get Answer, Expert's Help, Parameter default values Discussions

Write discussion on Parameter default values
Your posts are moderated
Related Questions
Example of GROUP BY and COLLECT Operator Example: Using GROUP BY and COLLECT to obtain C_ER2 SELECT CourseId, CAST ( COLLECT (ROW (StudentId, Mark)) AS ROW (Studen

Wrapping and unwrapping in SQL Operators WRAP and UNWRAP in connection with attributes whose declared types are tuple types. Example shows how extension and projection can be

Ending Transactions A good quality programming practice is to commit or roll back every transaction explicitly. Whether you rollback or issue the commit in your PL/SQL program

Albeit simple method : These all the truth tables give us our first as albeit simple method for proving a theorem: where check whether it can be written in propositional logic

Example of ADD CONSTRAINT in SQL Example: Alternative formulation for MAX_ENROLMENTS ALTER TABLE IS_ENROLLED_ON ADD CONSTRAINT MAX_ENROLMENTS CHECK ((SELECT COUNT (*)

Restriction and AND - SQL Restriction is available via the WHERE operator, and so it is in SQL. However, by Example showing how a certain simple restriction can be expressed u

Multiset types - SQL An SQL multiset is what in mathematics is also known as a bag-something like a set except that the same element can appear more than once. The body of an

Effects of NULL The numeric variable X, perhaps of type INTEGER, might be assigned NULL. In that case the result of evaluating X + 1 is NULL, and so SET Y = X + 1 assigns NULL

Need to change mysql query to PDO Project Description: I want someone to convert me 1 .php file that includes some sql/mysql stuff to PDO is a very small file. Skills requ

Definition of CROSS JOIN - SQL Let s = t1 CROSS JOIN t2, where t1 and t2 are table expressions optionally accompanied by range variables. Then: Note: Here T denotes Table