How to return two types in pl/sql oracle - oracle

How to return two types in pl/sql oracle?
return number -- here I tell than function reurns number, but i want to return number and string when an exception is thrown
is
prPrice number; -- product price
curFrom number; -- price
pgcount number := 0; -- product count
noProductsOnDate EXCEPTION;
wrongCurrency EXCEPTION;
begin
SELECT COUNT(*) INTO pgcount
from Products pr, Outgoing outg, Incoming inc
where pr.PROD_ID = outg.PROD_ID and pr.PROD_ID = inc.PROD_ID and inc.inc_date > d
Having sum(inc.quantity) > sum(outg.quantity);
if pgcount = 0 or pgcount is null then
raise noProductsOnDate;
END IF;
if curTo > 2 OR curTo < 1 then
raise wrongCurrency;
end if;
If curTo = 1
then curFrom := 2;
Elsif curTo = 2
then curFrom := 1;
END IF;
select pric.Value*cour.value into prPrice from Prices pric, Cources cour
where p = pric.prod_id and pric.DAYFROM <= d and (pric.DAYTO >= d or pric.DAYTO is null) and cour.cur_idto = curTo and cour.cur_idfrom = curFrom;
return prPrice; -- here i wanna return number
exception
when noProductsOnDate then return '1q'; -- here i wanna return string (error message)
when wrongCurrency then return '2q'; -- here i wanna return string (error message)
end;
I can't return string in exception, because function return number
Maybe I doing something wrong,
Please, tell me how can I return several data types from one function, maybe I should do everything differently, but I don't understand how to do it ((

You can't do it that way, but can another way around - declare function to return varchar2. Then, in your code, you can
create function ...
return varchar2
is
begin
... do whatever you're doing
return to_char(poPrice);
exception
when ... then return '1q';
end;

I write my functions often in this form:
return 0 in case of success
return a positive number in case of a warning (often not used)
return a negative number in case of an error
And I use out parameters for the function results, in your case the price and/or a message.
Something along the lines of:
CREATE OR REPLACE FUNCTION my_function(vi_date IN DATE,
vo_price OUT NUMBER,
vo_msg OUT VARCHAR) RETURN INTEGER IS
BEGIN
...
IF pgcount = 0 OR pgcount IS NULL THEN
vo_msg := 'No products on that date.';
RETURN -20001; -- return custom error code
END IF;
...
IF vo_price < 0 THEN
vo_msg := 'Price is negative. Calculation my be wrong.';
RETURN 1; -- return warning flag (positive number)
ELSE
RETURN 0; -- means success
END IF;
EXCEPTION WHEN OTHERS THEN
-- Error. Return negative number. SQLCODE is always the negative ORA code
-- except for ORA-1403 which is strangely SQLCODE 100 instead
vo_msg := SQLERRM;
RETURN CASE WHEN SQLCODE = 100 THEN -1403 ELSE SQLCODE END;
END my_function;
Then call it like this:
DECLARE
v_code INTEGER;
v_price NUMBER (10,2);
v_msg VARCHAR(1000);
BEGIN
v_code := my_function(DATE '2022-09-29', v_price, v_msg);
IF v_code >= 0 THEN
DBMS_OUTPUT.PUT_LINE('The price is: ' || v_price);
END IF;
IF v_code <> 0 THEN
DBMS_OUTPUT.PUT_LINE('Message: ' || v_msg);
END IF;
END;

Related

Oracle Apex Validation - Valid Product Name

I have a Validation beginner's question here:
I have a select list item, options:
Product
SKU
If the user selects the SKU option a new textfield Item is shown for the customer to write down the SKU number.
My validation then tries to prevent an invalid SKU to be inserted.
This is what I have so far:
declare
v_rows_approved_min number;
v_rows_approved_max number;
err varchar2(300);
begin
if :P8_PRODUCT_OR_SKU = 'SKU' -- THIS IS MY SELECT LIST ITEM
then
err := 'Not a valid SKU';
v_rows_approved_min := 1;
select count(*) into v_rows_approved_max from SKU_TABLE;
for cur_a in (select SKU from SKU_TABLE)
loop
exit when v_rows_approved_min > v_rows_approved_max;
if :P8_SKU = cur_a.SKU
then return err;
else null;
end if;
v_rows_approved_min := v_rows_approved_min + 1;
end loop;
else null;
end if;
end;
Not sure what's going on here, can anyone help please?
Thanks!
This code is overly complex. It checks if a page item value exists in a table and returns an error if a match is found. This is done with a loop with some additional logic to exit the loop with the number of iterations reaches the select count. That last logic is not needed. If a table contains 5 rows, then the loop will have 5 iterations. No need to do a SELECT count from the table (v_rows_approved_max) and then check every iteration if that number has not been reached yet...
Also, there is no RETURN statement if not match is found, so that is added at the end.
Here is an attempt at a rewrite:
DECLARE
---- not needed
-- v_rows_approved_min NUMBER;
-- v_rows_approved_max NUMBER;
err VARCHAR2(300) := 'Not a valid SKU';
BEGIN
IF :P8_PRODUCT_OR_SKU = 'SKU' -- THIS IS MY SELECT LIST ITEM
THEN
---- err can be defaulted in declaration
--err := 'Not a valid SKU';
--v_rows_approved_min := 1;
---- not needed see below
--SELECT COUNT(*) INTO v_rows_approved_max FROM sku_table;
FOR cur_a IN ( SELECT sku FROM sku_table ) LOOP
---- not needed. You're looping through the table, v_rows_approved_min will be > than v_rows_approved_max
--EXIT WHEN v_rows_approved_min > v_rows_approved_max;
IF :P8_SKU = cur_a.sku THEN
RETURN err;
---- not needed
-- ELSE
-- NULL;
END IF;
---- not needed
-- v_rows_approved_min := v_rows_approved_min + 1;
END LOOP;
---- not needed
-- ELSE
-- NULL;
END IF;
-- you need to return something whenever the function ends...
RETURN NULL;
END;
/
however...
This can be greatly simplified.
Create a validation of type "Rows returned"
Source:
SELECT
1
FROM
sku_table WHERE sku = :P8_SKU
Error Message: Not a valid SKU
Server Side condition (Type Item = Value): Item: P8_PRODUCT_OR_SKU; Value: SKU
This does exactly the same thing.

Comparing number with varchar2

I have this function and I need to compare number with varchar.
CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return number as
begin
declare odd integer;
declare i_perecentage=0;
begin
if i_odd ='SP'
then
return (0);
end if;
odd:=round(to_number((1-i_perecentage/100)*i_odd),2);
if odd<1
then
return(i_odd);
else
return(round(odd,2));
end if;
end;
end;
/
PS: I edited function and i resolve problem with comparing , now i have another situation that i dont like..
This function returns calculated percentage of i_odd. The problem is that if i pass 0 in i_percentage in results i get result with no decimal places(for example: i_odd = 3.10 and i_percentage = 0 i get odd = 3 but if I pass i_odd = 3.10 and i_percentage = 1 i get odd = 3.10 ).
Why is on i_percentage = 0 i dont get decimal places ??
If you want to validate a varchar2 field as a number in PL/SQL, typically you'd just try converting it to a number and catch the exception.
CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return number as
odd number;
BEGIN
-- if i_odd = 'SP' (or another non-number), this will throw an ORA-01722
-- exception which will be caught in the exception block, below
odd := to_number(i_odd); -- you might want a format mask here
--... now you can use "odd" as a number
EXCEPTION WHEN INVALID_NUMBER THEN
return 0;
END;
/
You can also nest a begin..end block in the middle of your code just to catch exceptions, if that works better for you:
CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return number as
odd number;
begin
begin
odd := to_number(i_odd); -- you might want a format mask here
exception when INVALID_NUMBER then
odd := 0;
end;
--... now you can use "odd" as a number
end;
/
The reason why you can't catch the invalid_number exception is because you are declaring the input parameter as a number. When you call your function, Oracle tries to convert the string to a number first (and it fails of course, before entering your code at all).
If you change the input parameter to varchar2, then the conversions to number (implicit in this case) is done inside the function, and invalid numbers can be caught and handled as you want (here I'm just returning a different string to denote the issue):
create or replace function is_odd_even(i_num in varchar2)
return varchar2
is
begin
-- conversion to number is done here
if (mod(i_num, 2) = 0) then
return 'EVEN';
else
return 'ODD';
end if;
exception
when INVALID_NUMBER or VALUE_ERROR then
-- do something meaningful
return 'INV';
end;
Usage example:
with x as (
select '1' as val from dual
union all
select 'SP' as val from dual
union all
select '2' as val from dual
)
select x.val, is_odd_even(x.val)
from x;
Output:
1 ODD
SP INV
2 EVEN
SOLUTION:
CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return varchar2 as
odd varchar2(10);
ret_value number(4);
begin
if (i_odd ='SP') or i_odd is null then
return 'SP';
else
odd :=ROUND( TO_NUMBER( ( 1 - (play_beting.get_odds_percentage(i_id) / 100 ) ) * TO_NUMBER(i_odd) ), 2);
IF(odd < 1) THEN
ret_value := TO_NUMBER(i_odd);
ELSE
ret_value := to_char(odd,'9999.00');
END IF;
END IF;
RETURN to_char(ret_value,'9999.00');
END getOdds;

PL/SQL exception handling in function

I am trying to generate DIVIDE_BY_ZERO exception in my oracle PL/SQL program. I using function but when I run my program I am getting error which is shown below. Can anyone tell me what is wrong in my program ?
code:
CREATE OR REPLACE
PACKAGE CALCULATOR AS
FUNCTION AddNumber(addend IN Number, Addend2 IN Number) RETURN NUMBER;
FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER;
END CALCULATOR;
/
CREATE OR REPLACE
PACKAGE BODY CALCULATOR AS
FUNCTION AddNumber(addend IN Number, Addend2 IN Number) RETURN NUMBER AS
BEGIN
return addend + addend2;
END AddNumber;
FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER AS
BEGIN
return divend / divend2;
EXCEPTION
WHEN ZERO_DIVIDE THEN
NULL;
END DivNumber;
END CALCULATOR;
/
select calculator.AddNumber(3,4) from dual;
select calculator.DivNumber(12,0) from dual;
output:
you can deal with exceptions like this
FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER AS
e_ZERO_DIVIDE EXCEPTION;
BEGIN
-- the condition
IF divend2 = 0 THEN
Raise e_ZERO_DIVIDE;
END IF;
return divend / divend2;
--Exception handling
EXCEPTION
WHEN e_ZERO_DIVIDE THEN
dbms_output.put_line('Division by 0 or null');
RETURN 1; -- or 0 or null
WHEN OTHERS THEN
dbms_output.put_line('ERROR: '||sqlerrm);
RETURN 1; -- or 0 or null
END DivNumber;
IMO you should never write code which throws an avoidable exception. There may be times when you can't avoid raising an exception but in this case you can. I suggest that you rewrite your function as follows:
FUNCTION DivNumber(numerator IN Number, denominator IN Number) RETURN NUMBER AS
BEGIN
IF NVL(denominator, 0) <> 0 THEN
return numerator / denominator;
ELSE
RETURN NULL;
END IF;
END DivNumber;
Best of luck.

Where in stored procedure I can make exit status?

I have oracle stored procedure where i check sender I'd,source system, and transaction number at the beginning of the procedure. Can I do it this way:
If Id != "aaa"
Exit -1;
Else if source = " ".
Exit -1;
Else if trans = " ".
Exit -1;
Else.
-- continues stored procedure
I appreciate any help
To rephrase your question more generally, you want a caller of your routine to know if something bad has happened inside it. There are (at least) three ways of doing this in PL/SQL.
Use an OUT parameter
Procedure cannot return a value, the way a function does, but it can set an output parameter:
CREATE PROCEDURE inner (p_id IN VARCHAR2(10), p_res OUT NUMBER)
IS
BEGIN
p_res := 0; -- default value
IF p_id = 'aaa' THEN
p_res := -1;
RETURN;
ELSE
-- do something
END IF;
END;
Then in the caller you would have:
DECLARE res NUMBER;
...
inner('aaa', res);
IF res = -1 THEN
-- panic!
END IF;
...
Use a function
Despite your seeming aversion to functions, this might be an option.
CREATE FUNCTION inner (p_id IN VARCHAR2(10))
RETURN NUMBER
IS
BEGIN
IF p_id = 'aaa' THEN
RETURN -1;
END IF;
-- do something
RETURN 0;
END;
Then in the caller:
...
IF inner('aaa') = -1 THEN
-- panic!
END IF;
...
Use an exception
Similar to other programming languages, PL/SQL has exceptions:
CREATE PROCEDURE inner (p_id IN VARCHAR2(10))
IS
BEGIN
IF p_id = 'aaa' THEN
RAISE_APPLICATION_ERROR(-20000, 'ID cannot be ''aaa''');
ELSE
-- do something
END IF;
END;
and in the caller:
...
DECLARE
panic EXCEPTION; -- declare exception
PRAGMA EXCEPTION_INIT (panic, -20000); -- assign error code to exception
...
BEGIN
inner ('aaa');
EXCEPTION
WHEN panic THEN
-- proceed to panic
END;
You are using a wrong syntax both for the IF...ELSE and for the exit.
Given that you are saying you need to get a return value, you probably need a function like this, using CASE:
create or replace function testFun ( pIn1 varchar2, pIn2 varchar2) return varchar2 is
begin
case
when pIn1 is null then
return -1;
when pIn2 = ' ' then
return -2;
else
return 999;
end case;
end;

Is it possible to use "return" in stored procedure?

CREATE PROCEDURE Pname(in_Tid IN VARCHAR2,in_IP IN VARCHAR2,outstaticip OUT VARCHAR2,outcount OUT NUMBER)
AS
BEGIN
select STATIC_IP into outstaticip from OP_TTER_MAPPING where TERMINAL_ID = in_Tid;
if in_IP = outstaticip then
return 1;
else
select COUNT(*) into outcount from OP_TTER_MAPPING where DYNAMIC_IP_LOW <= in_IP AND DYNAMIC_IP_HIGH >= in_IP AND TERMINAL_ID = in_Tid;
if outcount = 1 then
return 1;
else
return 0;
end if;
end if;
END;
Is it possible to use return in stored procedure like above?
If we can use return, how can i get that return value in Executesql("begin Pname(----)END") method
EDIT
Now I edited my return value in stored procedure like this, am I doing it right ?
CREATE PROCEDURE P_ValidateTIDIP(in_Tid IN VARCHAR2,in_IP IN VARCHAR2,outstaticip OUT VARCHAR2,outcount OUT NUMBER,outretvalue OUT NUMBER)
AS
BEGIN
select STATIC_IP into outstaticip from OP_TTER_MAPPING where TERMINAL_ID = in_Tid;
if in_IP = outstaticip then
outretvalue:=1;
else
select COUNT(*) into outcount from OP_TTER_MAPPING where DYNAMIC_IP_LOW <= in_IP AND DYNAMIC_IP_HIGH >= in_IP AND TERMINAL_ID = in_Tid;
if outcount = 1 then
outretvalue:=1;
else
outretvalue:=0;
end if;
end if;
END;
In Stored procedure, you return the values using OUT parameter ONLY. As you have defined two variables in your example:
outstaticip OUT VARCHAR2, outcount OUT NUMBER
Just assign the return values to the out parameters i.e. outstaticip and outcount and access them back from calling location. What I mean here is: when you call the stored procedure, you will be passing those two variables as well. After the stored procedure call, the variables will be populated with return values.
If you want to have RETURN value as return from the PL/SQL call, then use FUNCTION. Please note that in case, you would be able to return only one variable as return variable.
Use FUNCTION:
CREATE OR REPLACE FUNCTION test_function
RETURN VARCHAR2 IS
BEGIN
RETURN 'This is being returned from a function';
END test_function;
-- IN arguments : you get them. You can modify them locally but caller won't see it
-- IN OUT arguments: initialized by caller, already have a value, you can modify them and the caller will see it
-- OUT arguments: they're reinitialized by the procedure, the caller will see the final value.
CREATE PROCEDURE f (p IN NUMBER, x IN OUT NUMBER, y OUT NUMBER)
IS
BEGIN
x:=x * p;
y:=4 * p;
END;
/
SET SERVEROUTPUT ON
declare
foo number := 30;
bar number := 0;
begin
f(5,foo,bar);
dbms_output.put_line(foo || ' ' || bar);
end;
/
-- Procedure output can be collected from variables x and y (ans1:= x and ans2:=y) will be: 150 and 20 respectively.
-- Answer borrowed from: https://stackoverflow.com/a/9484228/1661078
It is possible.
When you use Return inside a procedure, the control is transferred to the calling program which calls the procedure. It is like an exit in loops.
It won't return any value.
CREATE PROCEDURE pr_emp(dept_id IN NUMBER,vv_ename out varchar2 )
AS
v_ename emp%rowtype;
CURSOR c_emp IS
SELECT ename
FROM emp where deptno=dept_id;
BEGIN
OPEN c;
loop
FETCH c_emp INTO v_ename;
return v_ename;
vv_ename := v_ename
exit when c_emp%notfound;
end loop;
CLOSE c_emp;
END pr_emp;

Resources