PL/SQL PLS-00103 on adding two variables - oracle

i'm new to pl/sql and trying to print even numbers up till 100 using the following code:
Declare
i number;
sum number:=0;
x number:=2;
n number;
Begin
for i in 1..100 loop
if (i%x=0) then
n:=i;
sum:=sum+n;
end if;
end loop;
dbms_output.put_line(sum);
end;
i get this error
ERROR at line 10:
ORA-06550: line 10, column 12:
PLS-00103: Encountered the symbol "+" when expecting one of the following:
(
ORA-06550: line 13, column 26:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
(
Help? :(
There is no % operator in PL/SQL; use the mod(i,x) function instead. Anyway, you don't need the n variable. sum:=sum+i will do. sum might be a reserved word, use s instead.
\
Now:
Declare
i number;
sum number:=0;
Begin
for i in 1..100 loop
if (mod(i,2)=0) then
sum:=sum+i;
end if;
end loop;
dbms_output.put_line(sum);
end;
sameproblem :(

There is no % operator in PL/SQL; use the mod(i,x) function instead. Anyway, you don't need the n variable. sum:=sum+i will do. But: sum is a PL/SQL keyword, use s instead.

Declare
i number;
sum1 number:=0;
x number:=2;
n number;
Begin
for i in 1..100 loop
if(i mod x =0)then
n:=i;
sum1:=sum1+n;
end if;
end loop;
dbms_output.put_line(sum1);
end;
/
Reason :
The "sum" is one of the aggregate function ,so we can't use this as variable name.

Related

PLS-00103: Encountered the symbol ")" when expecting one of the following: (

I am a beginner in oracle and following is my function definition and invocation part. I am unable to understand the error that I get when I call the function. Please help me rectify my code.
ORA-06550: line 4, column 56: PLS-00103: Encountered the symbol ")" when expecting one of the following: (
create or replace function totalcustomers
RETURN number
IS
total number:=0;
BEGIN
select count(*) into total from customers;
RETURN total;
END;
/
declare sum number;
BEGIN
sum := totalcustomers();
dbms_output.put_line('Total number of customers '||sum);
END;
/
Do not use sum as a variable which is a reserved keyword in Oracle.
Sum is a function, so it's expecting the open paren. Rename the variable.
The function invocation part was throwing the mentioned error, because "sum" might be a pre-defined keyword in oracle. Changing the variable as follows helped.
declare x number;
BEGIN
x:=totalcustomers();
dbms_output.put_line(' Total number of customers: '||x);
END;
/
Output :
Statement processed.
Total number of customers: 6

not able to delete an element from array

PLS-00306: wrong number or types of arguments in call to 'DELETE'
create or replace PACKAGE BODY MYPACKAGE AS
PROCEDURE LeaveDates4
(
I_STDATE IN DATE,
I_ENDDATE IN DATE,
O_DATES OUT DateArray
)
AS
n INTEGER := LEAST( I_ENDDATE - I_STDATE, 29 );
just_date DATE;
H_dates DateArray;
--cursor cur_holidates is select HOLIDAY_DATE from TSL_HOLIDAYLIST;
x number;
BEGIN
select count(*) into x from TSL_HOLIDAYLIST;
O_DATES := DateArray();
H_dates := DateArray();
O_DATES.EXTEND( n + 1 );
-- h_dates.extend;
dbms_output.put_line(n);
dbms_output.put_line(x);
FOR i IN 0 .. n LOOP
O_DATES(i+1) := I_STDATE + i;
dbms_output.put_line(I_STDATE + i);
END LOOP;
for cur in (select HOLIDAY_DATE from TSL_HOLIDAYLIST)
loop
h_dates.extend;
h_dates(h_dates.last):= cur.HOLIDAY_DATE;
end loop;
FOR i IN h_DATES.first..h_DATES.last LOOP
dbms_output.put_line(h_DATES(i)||i);
END LOOP;
for i in O_DATES.first..O_DATES.last LOOP
FOR j IN h_DATES.first..h_DATES.last LOOP
if o_dates(i)=h_dates(j)
then o_dates.delete(i); -- PLS-00306 error here
end if;
end loop;
end loop;
END LeaveDates4;
END MYPACKAGE;
The above code is failing with an error at o_dates.delete(i);
I want to delete the date from o_dates if it exists in h_dates.
The error I am getting is:
PLS-00306: wrong number or types of arguments in call to 'DELETE'
You can't delete elements from the middle of a varray.
PL/SQL Language Reference: PL/SQL Collections and Records: DELETE Collection Method
For a simpler example, define both a varray and a nested table collection:
create or replace type date_varray as varray(3) of date;
create or replace type date_ntab as table of date;
Now declare a collection of each type and try to deletion element from it.
Nested table:
declare
my_ntable date_ntab := date_ntab(sysdate -1, sysdate, sysdate +1);
begin
my_ntable.delete(2);
end;
PL/SQL procedure successfully completed.
Varray:
declare
my_varray date_varray := date_varray(sysdate -1, sysdate, sysdate +1);
begin
my_varray.delete(2);
end;
my_varray.delete(2);
*
ERROR at line 4:
ORA-06550: line 4, column 5:
PLS-00306: wrong number or types of arguments in call to 'DELETE'
ORA-06550: line 4, column 5:
PL/SQL: Statement ignored

PL/SQL For & When Error

This is my code,
Declare
For num IN 1..10 LOOP
Continue When Mod(num,2)!=0;
DBMS_OUTPUT.PUT_LINE(num);
END LOOP;
END;
/
I am getting the following error:
SQL> # E:\dbms\f7.sql
For num IN 1..10 LOOP
*
ERROR at line 2:
ORA-06550: line 2, column 1:
PLS-00103: Encountered the symbol "FOR" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The symbol "begin" was substituted for "FOR" to continue.
ORA-06550: line 3, column 10:
PLS-00103: Encountered the symbol "WHEN" when expecting one of the following:
:= . ( # % ;
Please someone give me a working code so I can execute !!!
You don't need the declare block since you aren't declaring any variables, but you're missing the begin keyword:
BEGIN -- Here
FOR num IN 1..10 LOOP
Continue When Mod(num,2)!=0;
DBMS_OUTPUT.PUT_LINE(num);
END LOOP;
END;
/

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().

when i am trying to sort the array its giving an error

DECLARE
TYPE myarray is varray(10) of pls_integer;
unsorted myarray := myarray();
min pls_integer;
begin
unsorted := myarray(2,5,8,6,4,9,1,3,7,10);
FOR i in 1 .. unsorted.count-1 loop
begin
for j in i+1 .. unsorted.count loop
begin
if(unsorted(j)<unsorted(i))
then
begin
min := unsorted(i);
unsorted(i) := unsorted(j);
unsorted(j) := min;
end;
end if;
end;
end loop;
dbms_output.put_line(unsorted(i));
end;
end loop;
end;
my code giving an error i am not able to understand why?
ORA-06550: line 16, column 31:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
You use min as a variable, but min is a reserved keyword. Use another variable name instead, and it should be working.

Resources