Overloading issue in pl/sql - oracle

Guys I am trying to overload a function within a package in Oracle 11gR2.
Functions go like this.
Function is_h2h(p_param in number) return boolean
is
begin
--some code here
return true;
end;
Function is_h2h(p_param in number) return number
is
begin
--some code here
return 1;
end;
So the problem is when I write an if statement within another procedure for example
if is_h2h(some_param) then --code goes
end if;
the compiler returns PLS-00307: Too many declarations of 'IS_H2H' match this call.
So how can I implement this kind of overloading?

You need to make the signatures different (ignoring the return type) otherwise the compiler won't choose for you.
My first preference would be to give the functions different names, e.g.:
function is_h2h_b(p_param in number) return boolean...
function is_h2h_n(p_param in number) return number...
If you really really really want to use the same name, you can use a different parameter name - but then you're limited to only using named parameters (which, mind you, is good practice anyway):
function is_h2h(p_param_b in number) return boolean...
function is_h2h(p_param_n in number) return number...
if is_h2h(p_param_b => some_param) then...

Related

Oracle Apex Checkbox Group Limit

This might be a silly one but haven't been able to figure it out.
I have a Checkbox Group Item (with Static info) with 4 different options, I want to limit the amount of checked boxes to just two. Is this something possible to make?
Thank you
A checkbox is submitted as a colon-separated string. APEX_STRING has lots of functionality to convert the string to a pl/sql collection (and back). Once converted you can use functions like FIRST, LAST, COUNT. Or even compare collections using INTERSECT. For checking a max nr, a COUNT is enough.
So the validation would be something like this (type Function Body returning Error Text):
DECLARE
l_arr apex_t_varchar2;
BEGIN
l_arr := apex_string.split(:P13_CHECKBOX,':');
IF l_arr.COUNT > 2 THEN
RETURN 'Can only select 2 values';
ELSE
RETURN NULL;
END IF;
END;

Assign a value to a variable on RETURN PL/SQPL

Is there a way to achieve this in PL/SQL oracle ?
RETURN (return_status:=1);
It gives a compilation error when I try to do this. If this is not possible please suggest a better alternative instead of doing
return_status := 1;
RETURN (return_status);
When we execute a RETURN the procedure terminates at that point and control flow passes to the calling program. So there would be no value in this construct …
RETURN (return_status:=1);
… because nothing in the program unit could act on return_status after the RETURN.
this is a function and return_status is an OUT param
That's the root of your problem: poor design. Either return a value or have it as an OUT parameter but not both. The accepted practice in PL/SQL is that a function returns a value and has no OUT parameters. Only procedures (which don't have RETURN) have OUT parameters.
So you choices are:
return 1 and don't have an OUT parameter
set OUT parameter = 1 and return something else
make it a procedure instead
I dont understand what the problem is?
If you want to return the value return_status then the second option is just fine(if you are really doing a hardcoded assignment you could just return 1.
And I thought maybe you actually have an external variable return_status you are trying to change the value of by calling this function. In which case, use a procedure and have return_status be an IN OUT variable(maybe even just OUT).
AFAIK, you cannot assign value in the Oracle FUNCTIONS RETURN Statement.
From Oracle Docs, the syntax should be
RETURN [[(] expression [)]];
For expression refer Expressions
Only solution i can think of based on your requirement(1 condition instead of 2) is using case expression. Something like below (if you have any condition)
RETURN
CASE when return_status is not null
THEN 1
Functions with OUT parameters are permitted but smell wrong.

PL/SQL difference between DEFAULT and assignment operator

I'm newbie in PL/SQL and this question might seem to be 'childish' so I'm sorry in advance, but Google didn't help me at all...
Is there any difference between following procedures?
Procedure p1(a Number DEFAULT 0) Is
Begin
DBMS_OUTPUT.put_line(a);
End;
Procedure p2(a Number := 0) Is
Begin
DBMS_OUTPUT.put_line(a);
End;
I have checked them, so when I call both of them without args the output is 0, but I'm not sure if there are any side effects.
If you follow Oracle's documentation
You can use the keyword DEFAULT instead of the assignment operator to
initialize variables. You can also use DEFAULT to initialize
subprogram parameters, cursor parameters, and fields in a user-defined
record.
Use DEFAULT for variables that have a typical value. Use the
assignment operator for variables (such as counters and accumulators)
that have no typical value.
DECLARE
blood_type CHAR DEFAULT 'O'; -- Same as blood_type CHAR := 'O';
hours_worked INTEGER DEFAULT 40; -- Typical value
employee_count INTEGER := 0; -- No typical value
BEGIN
NULL;
END;
/
So I guess internally is the same.
Let's take a look to the documentation of the latest Oracle release (12c R1) at the moment of writing. You explicitly ask for subprogram parameters so let's take that first.
Default Values for IN Subprogram Parameters:
When you declare a formal IN parameter, you can specify a default value for it. A formal parameter with a default value is called an optional parameter, because its corresponding actual parameter is optional in a subprogram invocation. If the actual parameter is omitted, then the invocation assigns the default value to the formal parameter.
The documentation doesn't mention default keyword but it still works. Example (in 12c R1):
declare
function f1(a in number) return number is begin return a; end;
function f2(a in number := 2) return number is begin return a; end;
function f3(a in number default 3) return number is begin return a; end;
begin
dbms_output.put_line(f1(1));
dbms_output.put_line(f2);
dbms_output.put_line(f3);
end;
/
Prints 1, 2, 3 as expected.
However as it's not mentioned in the authorative documentation I discourage the use of default keyword in this context.
Other interesting context is Initial Values of Variables and Constants:
To specify the initial value, use either the assignment operator (:=) or the keyword DEFAULT, followed by an expression.
And that's the only time default keyword is mentioned. All documentation examples use assigment only.
Conclusion
Technically default keyword and assigment are the same and work in both contextes, but only assigment is promoted in the documentation. I think Oracle is effectively deprecating the default keyword in this context and thus I won't recommend the use of it in new PL/SQL code. Assigment operator makes the same job with zero fuss.

Oracle PL/SQL Function - caller declares parameter values => function returns equation solution

I want to create a function where a caller can declare 4 parameters and PL/SQL will solve an equation that uses these parameters. My current code is:
create or replace function get_distance(
p_y1 in number,
p_x1 in number,
p_y2 in number,
p_x2 in number)
return number
as
begin
return SQRT(power(p_x2 - p_x1) + power(p_y2 - p_y1));
end;
I'm pretty sure the error is in the return-statement but this far I haven't been able to figure it out.
function power() accepts two parameters.
The base and the exponent.
power(base, exponent)
like, power(100, 2) = 10000
So, your power function should be like power(p_x2 - p_x1, 2), I mean your are missing second parameter in the function power() , second parameter should be a value according to your logic.
For more Detail Click : Oracle Power Function

Weird runtime error while implementing a bubble sort in Pascal

This snippet not only causes a runtime error, it makes FPC close if I run it using the debugger.
procedure sortplayersbyscore(var vAux:tplayers);
procedure swap(var a:trplayers;var b:trplayers);
var
rAux:trplayers;
begin
rAux:=a;
a:=b;
b:=rAux;
end;
var
i,j:integer;
sorted:boolean;
begin
vAux:=playersarray;
i:=1;
sorted:=false;
while (i <= MAXPLAYERS -1) and not sorted do
begin
j:=1;
sorted:=true;
while (j <= MAXPLAYERS -i) do
begin
if (vAux[j].score < vAux[j+1].score) then
begin
swap(vAux[j],vAux[j+1]);
sorted:=false;
end;
inc(j);
end;
inc(i);
end;
end;
The code itself is part of a really big source file, I can post the whole thing but the responsible for the error is just that bunch of lines. The debugger terminates at line:
swap(vAux[j],vAux[j+1]);
tplayers is just a type defined as an array of records that contain score (an integer) among a bunch of other variables. trplayers is the type of the aforementioned records. I'm at a total loss; FPC (while not under debugging mode) spits an out-of-range error but under my watches I see that the variables I'm trying to read exist. Any help is really appreciated!
rAux:trplayers; have you typed a wrong symbol or the type here really contains "r" in its name?
It looks valid (other than typos) ... so let's try something simple.
What's the value of "j" when you abort?
If the debugger won't tell you, try adding:
writeln ('j = ', j);
just before the "swap" call.
As Yochai's question implied, your array needs to be dimensioned at least from
1 (or lower) to MAXPLAYERS (or larger). (I.e.: 0..MAXPLAYERS-1 would not work,
but 1..MAXPLAYERS should.)

Resources