how to execute an arithmetic program in PL/SQL? - oracle

I am new to PL/SQL so I have tried to execute some PL/SQL programs in SQL developer. But it throws an error, even my program was correct. Is there any need to change my SQL Developer settings to perform mathematical operations?
I was tying to execute this program.
DECLARE
num number(5):= 20;
num1 number(5):= 80;
sum number(5);
BEGIN
sum:= num + num1;
dbms_output.put_line('result='||sum);
END;
and the error was :
Error starting at line : 2 in command -
Error report -
ORA-06550: line 7, column 36:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
(
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

You are using reserved keyword sum as identifier which is not allowed.
Try this:
DECLARE
num number(5):= 20;
num1 number(5):= 80;
v_sum number(5);
BEGIN
v_sum:= num + num1;
dbms_output.put_line('result='||v_sum);
END;

Related

ORACLE PLS-00103:, ORA-06550: := . ( # % ;

I've got a problem with procedure:
create or replace
PROCEDURE SOLVER AS
IS_ACTIVE "Parameter"."Value"%TYPE;
BEGIN
BEGIN
SELECT "Value" INTO IS_ACTIVE from "Parameter" WHERE "Name" = 'ARCHIVER';
EXCEPTION
WHEN OTHERS THEN
IS_ACTIVE:='OFF';
END;
When I try to run it, I gets an Error:
Error report:
ORA-06550: line 3, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
:= . ( # % ;
The symbol ";" was substituted for "END" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
Version 3.2.09
Main Build 09-30
The error message strongly suggests you are getting the error when you call the procedure, not when you create it (and you did refer to 'when I try to run it'); so you are doing this:
begin
solver
end;
/
which generates exactly that error.
If you add a semicolon it will work:
begin
solver;
end;
/
db<>fiddle

Get the first value from Oracle cursor - Calling From Java Code

I have a oracle cursor which I have created to facilitate concurrency. This is my cursor.
create or replace FUNCTION get_unlocked_records RETURN table_to_test%ROWTYPE IS
CURSOR c IS SELECT * FROM table_to_test where status_code = 5 FOR UPDATE SKIP LOCKED;
record_to_get table_to_test%ROWTYPE;
BEGIN
OPEN c;
FETCH c INTO record_to_get;
CLOSE c;
RETURN record_to_get;
END;
When I do the testing in 2 separate sql sessions using these commands,it gives the following errors.
declare
record_to_gets table_to_test%ROWTYPE;
begin
exec :record_to_gets := get_unlocked_records;
dbms_output.put_line(record_to_gets);
end;
Error
Error starting at line : 32 in command -
declare
record_to_gets table_to_test%ROWTYPE;
begin
exec :record_to_gets := get_unlocked_records;
dbms_output.put_line(record_to_gets);
end;
Error report -
ORA-06550: line 4, column 7:
PLS-00103: Encountered the symbol "" when expecting one of the following:
:= . ( # % ;
The symbol ";" was substituted for "" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What is the error that I am doing here ?
Since my ultimate goal is to call the function and get the result in java, how to call this function to get the first record in java ?
Thanks in advance.
EXEC[UTE] is a SQL*Plus command and prepending variable with a colon is done in SQL*Plus, but in PL/SQL EXECUTE IMMEDIATE might be used whereas that's not needed in your case, only using such an assignment without prepending the local variable is enough :
DECLARE
record_to_gets table_to_test%ROWTYPE;
BEGIN
record_to_gets := get_unlocked_records;
DBMS_OUTPUT.PUT_LINE(record_to_gets.col1);
DBMS_OUTPUT.PUT_LINE(record_to_gets.col2)
END;
/

I don't understand what is the problem in my store procedure

Create or replace PROCEDURE SSp_EmpHoursInfo
(p_EHrsInfo OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_EHrsInfo FOR
Select
a.personid, a.first_name, a.last_name,c.hoursworked,d.carecentername, c.break
from person a
join employee b on a.personid = b.empersonid
join employee_assigned_care_center c on b.empersonid = c.empersonid
join care_center d on c.empersonid = d.carecenterid
where hoursworked> 10;
END SSp_EmpHoursInfo;
Everytime I am trying to call the store procedure it is giving me this error msg:
Error starting at line : 225 in command -
BEGIN SSp_EmpHoursInfo (hoursworked> 10); END;
Error report -
ORA-06550: line 1, column 3:
PLS-00201: identifier 'HOURSWORKED' must be declared
ORA-06550: line 1, column 52:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The problem is very obvious.
BEGIN SSp_EmpHoursInfo (hoursworked> 10); END; is not the proper way of calling it.
The procedure parameter must be the sys_refcursor.
You must use:
DECLARE
OUTPUT_CUR SYS_REFCURSOR;
BEGIN
SSp_EmpHoursInfo (OUTPUT_CUR );
-- USE CURSOR ACCORDINGLY
END;
/

X is not a legal cursor attribute plsql

I am trying to find the gcd of two numbers using a recursive approach.
My function:
create or replace function gcd(a in number, b in number)
return number
as
begin
if a = 0
then return b;
else return gcd(b % a, a);
end if;
end;
/
I am calling it like this
declare
a1 number;
b1 number;
z number;
begin
a1:=25;
b1:=40;
z := gcd(a1,b1);
dbms_output.put_line(z);
end;
It throws me this error:
Error report -
ORA-06550: line 15, column 14:
PLS-00208: identifier 'X' is not a legal cursor attribute
ORA-06550: line 15, column 4: PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
b % a is not valid Oracle syntax. If you want to calculate a modulus you need to use the Oracle MOD() function, that is mod(b, a).
You get the PLS-00208 error because Oracle uses the % symbol to reference cursor attributes like %rowtype or %notfound. Find out more.

PL/SQL : i have a function but there is an error : "in a procedure,RETURN can not contain an expression"

Here is my code:
CREATE OR REPLACE FUNCTION customer_city_function(city_in IN VARCHAR2)
RETURN NUMBER
AS
number_cus NUMBER := 0;
CURSOR cus_cur IS
SELECT COUNT(*)
FROM customer
WHERE customer_city = city_in;
BEGIN
IF city_in IS NOT NULL THEN
OPEN cus_cur;
FETCH cus_cur INTO number_cus;
CLOSE cus_cur;
END IF;
RETURN number_cus;
END;
/
and here is warnings:
Error starting at line : 1 in command -
CREATE OR REPLACE FUNCTION customer_city_function(city_in IN VARCHAR2)
RETURN NUMBER
AS
number_cus NUMBER := 0
Error report -
SQL Command: functıon CUSTOMER_CITY_FUNCTION
Failed: Warning: executing is completed with a warning
Error starting at line : 5 in command -
CURSOR cur_cur IS
Error report -
Unknown Command
Error starting at line : 6 in command -
SELECT COUNT(*)
FROM costumer
WHERE customer_city=city_in
Error at Command Line : 8 Column : 25
Error report -
SQL Error: ORA-00904: "CITY_IN": undefined variable
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error starting at line : 9 in command -
BEGIN
IF city_in IS NOT NULL
THEN
OPEN cus_cur;
FETCH cus_cur INTO number_cus;
CLOSE cus_cur;
END IF;
RETURN (number_cus);
END;
Error report -
ORA-06550: row 2, column 6:
PLS-00201: 'CITY_IN' variable should been defined
ORA-06550: row 2, column 3:
PL/SQL: Statement ignored
ORA-06550: row 8, column 1:
PLS-00372: in a procedure, RETURN can not contain an expression
ORA-06550: row 8, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Where is my mistake? I can't find it, it doesn't make any sense.
(I translated this warning message from my language. I hope I did it right.)
I have just tried it in Command Window and it works. Why doensn't it work in Oracle SQL Developer sql worksheet?
There is nothing wrong with your posted code. The issue might be with your client or the way you are compiling the code.
As you have mentioned PL/SQL Developer in the tags, it might be possible that you have some extra characters in the SQL Worksheet and you are compiling the function as a script, thus the compiler finds it erroneous.
Here is a demo in SQL*Plus, and there is no error:
SQL> CREATE OR REPLACE FUNCTION customer_city_function(i_deptno IN number)
2 RETURN NUMBER
3 AS
4 number_cus NUMBER := 0;
5 CURSOR cus_cur IS
6 SELECT COUNT(*)
7 FROM emp
8 WHERE deptno=i_deptno;
9 BEGIN
10 IF i_deptno IS NOT NULL
11 THEN
12 OPEN cus_cur;
13 FETCH cus_cur INTO number_cus;
14 CLOSE cus_cur;
15 END IF;
16 RETURN number_cus;
17 END;
18 /
Function created.
SQL> sho err
No errors.
SQL> SELECT customer_city_function(10) FROM DUAL;
CUSTOMER_CITY_FUNCTION(10)
--------------------------
3
SQL>
The only difference in my code is that I have used EMP table instead of CUSTOMERS table and the input parameter is DEPTNO instead of CITY_IN. Rest everything is same and function compiles and executes without any errors.

Resources