Encountered the symbol "2" - oracle

In this procedure I want to make a list of all procedures in my code and the user chooses a number to execute a certain procedure or function.
Here's my code
CREATE OR REPLACE PROCEDURE calling
IS
chosen VARCHAR2(1);
V_count NUMBER;
BEGIN
&chosen;
IF chosen='1' THEN
V_count:=visited.count_nb_city_visited(&idclient);
ELSIF chosen='2' THEN
V_count:=visited.max_vis;
visited.extract_best(V_count);
ELSIF chosen='3' THEN
ORDER_ORIGIN_CITY.ORDER_CITY;
ELSIF chosen='4' THEN
ORDER_ORIGIN_CITY.ORDER_CLIENT;
END IF;
END;
When running the code, this warning pops up "PLS-00103: Symbol "2" encountered (if I choose the number 2 and the same error is shown with any number I choose)

You can't (shouldn't) use substitution variables in a stored procedure as it will substitute the values in on compilation (and not as you are probably expecting on execution).
So if you substitute 2 for &chosen and 42 for &idclient then your code will be compiled as:
CREATE OR REPLACE PROCEDURE calling
IS
chosen VARCHAR2(1);
V_count NUMBER;
BEGIN
2;
IF chosen='1' THEN
V_count:=visited.count_nb_city_visited(42);
ELSIF chosen='2' THEN
V_count:=visited.max_vis;
visited.extract_best(V_count);
ELSIF chosen='3' THEN
ORDER_ORIGIN_CITY.ORDER_CITY;
ELSIF chosen='4' THEN
ORDER_ORIGIN_CITY.ORDER_CLIENT;
END IF;
END;
And will always execute with those fixed values.
You are getting the error because 2; is not a valid PL/SQL statement.
Instead, you should pass all the bind variables in the signature. Something like:
CREATE OR REPLACE PROCEDURE calling (
chosen IN NUMBER,
idclient IN NUMBER
)
IS
V_count NUMBER;
BEGIN
IF chosen = 1 THEN
V_count:=visited.count_nb_city_visited(idclient);
ELSIF chosen = 2 THEN
V_count:=visited.max_vis;
visited.extract_best(V_count);
ELSIF chosen = 3 THEN
ORDER_ORIGIN_CITY.ORDER_CITY;
ELSIF chosen = 4 THEN
ORDER_ORIGIN_CITY.ORDER_CLIENT;
END IF;
END;
/
If you want to call it from an anonymous PL/SQL block, then you can use substitution variables in that block:
BEGIN
calling(
chosen => &chosen,
idclient => &idclient
);
END;
/

Related

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;

Can PL/SQL procedures call themselves recursively?

Using PL/SQL, it is possible to call a stored function from within that same function. This can be demonstrated with the following example:
CREATE OR REPLACE FUNCTION factorial(x in number)
RETURN number
IS
f number;
BEGIN
IF x = 0 THEN
f := 1;
ELSE
f := x * factorial(x-1);
END IF;
RETURN f;
END;
/
DECLARE
num number;
factorial number;
BEGIN
num := &num;
factorial := factorial(num);
dbms_output.put_line(' The factorial of '|| num || ' is ' || factorial);
END;
/
Can this be done using PL/SQL stored procedures as well?
Yes, you can write a procedure that calls itself recursively in PL/SQL. Here is an example - implementing the factorial.
With that said, don't ever write a procedure (or a function like yours) without error handling. If you don't understand why, change 5 to 5.3 in the anonymous block below, and you'll see why.
CODE window:
create or replace procedure fact ( x in number, x_fact out number )
as
begin
if x = 0 then x_fact := 1;
else fact(x-1, x_fact);
x_fact := x * x_fact;
end if;
end;
/
set serveroutput on
declare
z number;
begin
fact(5, z);
dbms_output.put_line(z);
end;
/
SCRIPT OUTPUT window (matching each "result" to the corresponding part of the code left as an exercise):
Procedure FACT compiled
PL/SQL procedure successfully completed.
120
You can certainly call PL/SQL functions recursively (with all the usual warnings about the dangers of doing so in any language!).
You are, however, going to run into trouble if you name a local variable the same as your function. You will, for example, get this error when you try to execute the block:
PLS-00222: no function with name 'FACTORIAL' exists in this scope

PLS-00201: identifier must be declared in Procedure

I have a PL/SQL Procedure code, which runs when it is / , but doesn't runs when it's executed. The error message I get is
SQL> EXECUTE MAXINUM;
BEGIN MAXINUM; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'MAXINUM' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
The code which I'm working on is :
DECLARE
N NUMBER;
M NUMBER;
O NUMBER;
P NUMBER;
X NUMBER;
PROCEDURE MAXINUM(N IN NUMBER, M IN NUMBER, O IN NUMBER, P IN NUMBER, X OUT NUMBER) IS
BEGIN
IF N>M AND N>O AND N>P THEN
X:=N;
ELSIF M>N AND M>O AND M>P THEN
X:=M;
ELSIF O>N AND O>M AND O>P THEN
X:=O;
ELSIF P>N AND P>M AND P>O THEN
X:=P;
END IF;
END;
BEGIN
N:=&NUMBER;
M:=&NUMBER;
O:=&NUMBER;
P:=&NUMBER;
MAXINUM(N,M,O,P,X);
DBMS_OUTPUT.PUT_LINE('HIGHEST NUMBER = '||X);
END;
/
When it gave the 'identifier error', I tried dropping this procedure I got the error:
SQL> DROP PROCEDURE MAXINUM;
DROP PROCEDURE MAXINUM
*
ERROR at line 1:
ORA-04043: object MAXINUM does not exist
I have so far read this, this, this solutions and other solutions some what related to this error.
You have written an anonymous block with a local procedure MAXINUM(). This procedure can be called within that block but does not exist outside that block. Consequently you cannot call it independently.
If you want to use the procedure elsewhere you need to create it as a first class database object:
create or replace procedure MAXINUM
(N IN NUMBER, M IN NUMBER, O IN NUMBER, P IN NUMBER, X OUT NUMBER)
is
BEGIN
IF N>M AND N>O AND N>P THEN
X:=N;
ELSIF M>N AND M>O AND M>P THEN
X:=M;
ELSIF O>N AND O>M AND O>P THEN
X:=O;
ELSIF P>N AND P>M AND P>O THEN
X:=P;
END IF;
END;
/
Now you can call it in your code, like this:
DECLARE
N NUMBER;
M NUMBER;
O NUMBER;
P NUMBER;
X NUMBER;
BEGIN
N:=&NUMBER;
M:=&NUMBER;
O:=&NUMBER;
P:=&NUMBER;
MAXINUM(N,M,O,P,X);
DBMS_OUTPUT.PUT_LINE('HIGHEST NUMBER = '||X);
END;
/
Points to note:
what happens if a parameter is null?
what happens if two arguments have the same value?
the convention would be to declare this as a function and return the highest value instead of setting an OUT parameter.
Incidentally I assume you're doing this as an exercise, as it is a re-implementation of an existing Oracle built-in function, greatest().

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;

PLS-00231 for in procedure defined function

I looked up the problem but didn't find a solution. my problem is as follows
create or replace procedure GETINFO(
pIVR OUT NUMBER
) IS
FUNCTION IS_MASK_ALLOWED(
pBusSeg IN NUMBER,
pCHTPMask IN NUMBER
)
RETURN BOOLEAN
IS
BEGIN
IF pCHTPMask > 0 THEN
ELSE RETURN TRUE;
END IF;
END;
BEGIN
SELECT 1
INTO pIVR
FROM DUAL
WHERE IS_MASK_ALLOWED(1, 2)=TRUE;
END;
the compiler says that I can not use IS_MASK_ALLOWED in SQL statements
ORACLE 10g
PL/SQL developer 7.5
Oracle does not support boolean statements in SQL so using True will not work. Try select 1 from dual where True = True to confirm.
Secondly if you're not allowing anyone to access is_mask_allowed outside your procedure and that's all it does then you can do this pIVR := 1. which'll have the same result as your code ( procedure and function ). Personally if this is somewhere near all they do I'd have everything in the same procedure.
If you're missing some information in is_mask_missing, that then with nothing after it maybe then your package will error on no_data_found as there is the possibility of select 1 from dual where 1 = 0, which returns nothing.
If you do want is_mask_allowed to be accessed outside your procedure it would be easiest to put it in a package. As you can't use a boolean I've used a binary, where 0 represents false and 1 true
create or replace package my_package is
function is_mask_allowed ( pBusSeg IN NUMBER
, pCHTPMask IN NUMBER
) RETURN NUMBER;
procedure get_info ( pIVR out NUMBER );
end my_package;
/
show error
create or replace package body my_package is
function is_mask_allowed ( pBusSeg IN NUMBER
, pCHTPMask IN NUMBER
) RETURN NUMBER is
begin
if pCHTPMask > 0 THEN
-- do something
return 0
else return 1
end if;
end is_mask_allowed;
-----------------------------------
procedure getinfo( pIVR OUT NUMBER ) IS
begin
if is_mask_allowed(1,2) = 1 then
pIVR := 1;
else
pIVR := -- something else;
end if;
end getinfo;
end my_package;
/
show error
You need to put your is_mask_allowed function into a package to use it in SQL, and you should return something other than a boolean (perhaps a number) from the function and test for that, so your SQL is something more like:
select my_pkg.is_mask_allowed(1, 2) into pIVR from dual;
Here is a recent discussion in the Oracle forums on the same error:
https://forums.oracle.com/forums/thread.jspa?threadID=2319500

Resources