X is not a legal cursor attribute plsql - oracle

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.

Related

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;
/

PL/SQL compilation error with declaring variable

I am trying to create a basic PL/SQL query where I am using a certain SKU as a parameter so that I can reference it without typing in the sku each time.
When compiling my code I get the error:
Error report:
ORA-06550: line 6, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Here is my code:
declare
myitem number (20);
begin
myitem := 1000956;
select f.order_no
from fdt_maptool f
where f.item = myitem;
end;
/
I thought that maybe it had to do with me using VARCHAR(20) instead of NUMBER, So I tried that as well.
declare
myitem number;
begin
myitem := 1000956;
select f.order_no
from fdt_maptool f
where f.item = myitem;
end;
/
And then received this error:
Error report:
ORA-06550: line 6, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I am fairly new to PL/SQL so if please go easy on me!
Within PLSQL you need to select into
declare
myitem number(20);
myorder number(20);
begin
myitem := 1000956;
select f.order_no
into myorder
from fdt_maptool f
where f.item = myitem;
end;
/
Now there's two standard things that might go wrong. You may not find a record or you may find more than one record. You need exception handlers to handle these cases.
declare
myitem number(20);
myorder number(20);
begin
myitem := 1000956;
select f.order_no
into myorder
from fdt_maptool f
where f.item = myitem;
exception
when no_data_found then
dbms_output.put_line('No record with this ID');
-- Only needed when not selecting a unique column.
when too_many_rows then
dbms_output.put_line('More than one record with this ID');
end;
/
Note that the too_many_rows exception is usually already covered by the fact that you are selecting an ID column that has a unique constraint defined on it.

how to execute an arithmetic program in PL/SQL?

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;

PL SQL Stored Procedure for new Transaction and order

I'm giving a small snippet of my code but I 'm receiving the below error when trying to create a new order for a stored Oracle pl sql procedure.
line 83 is the insert statement in the code and line 84 is in the insert part of the statement.
83/5 PL/SQL: SQL Statement ignored
84/47 PL/SQL: ORA-00984: column not allowed here
BEGIN
--Initializing values for variables
x_rowcount := 0;
x_stockonhand := 0;
Totaldue := 0;
--Total due calculation
--(price of phone*quantity + shipping cost)*1.06 (assuming 6% sales tax)
Totaldue := (((i_price * c_p_qty) + i_shipping_cost) * 1.06);
SAVEPOINT start_transaction; -- mark a savepoint
--INSERT a new record into order table.
INSERT INTO orders(o_id,c_id,p_id,s_id,order_date,o_qty,order_total,card_type,cc_number,exp_date,shipping_status)
VALUES (orders_seq.nextval, c_c_id,c_p_id,s_id,sysdate,c_p_qty,Totaldue,c_card_type,c_cc_number,c_exp_date,'Not shipped yet');
Check your declaration section. Usually this error appears when you make a typo in variable name or variable not declared. For example:
SQL> create table tmp (id number, str varchar2(100));
Table created.
SQL> declare
a number;
begin
insert into tmp (id, str)
values (a, a1);
end;
/
values (a, a1);
*
ERROR at line 5:
ORA-06550: line 5, column 14:
PL/SQL: ORA-00984: column not allowed here
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
The error is because one or more of the values in your VALUES(...,...) section is invalid.
I would suggest checking each one to see that they are valid. For example, is c_c_id declared and given a value somewhere else in the code? If not, this is likely your problem. Each one needs to be declared and given a value before it can be put in the VALUES(...,...) section of the INSERT statement.
INSERT INTO orders(o_id,c_id,p_id,s_id,order_date,o_qty,order_total,card_type,cc_number,exp_date,shipping_status)
VALUES (orders_seq.nextval, c_c_id,c_p_id,s_id,sysdate,c_p_qty,Totaldue,c_card_type,c_cc_number,c_exp_date,'Not shipped yet');

Resources