Need help understanding Cursor for loop - oracle

I am trying to write a code for every stock value that is $75 or more
add a "*" in the STK_FLAG column. my error repots are: Error report -
ORA-06550: line 15, column 21: PLS-00201: identifier 'STK_FLG' must be
declared ORA-06550: line 15, column 5: PL/SQL: SQL Statement ignored
ORA-06550: line 23, column 7: PL/SQL: ORA-00904: "STK_FLG": invalid
identifier ORA-06550: line 17, column 5: PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Blockquote
SET SERVEROUTPUT ON
DECLARE
CURSOR CURR
IS
SELECT STK_FLAG
FROM MM_MOVIE
WHERE MOVIE_VALUE * MOVIE_QTY >= 75
FOR UPDATE;
BEGIN
OPEN CURR;
LOOP
FETCH CURR INTO STK_FLG;
UPDATE
MM_MOVIE
SET
STK_FLG= '*'
WHERE
CURRENT OF CURR;
EXIT
WHEN CURR%notfound;
END LOOP;
Commit;
CLOSE CURR;
END;
/

You didn't declare a cursor variable (so there's nowhere you can put values returned by the cursor). Don't name it as column name; use a prefix, such as v_ or l_ or whichever you want.
Furthermore, in UPDATE you referenced a non-existent column. Cursor suggests its name is stk_flag, not stk_flg
Therefore, code that might be OK is
DECLARE
CURSOR curr IS
SELECT stk_flag
FROM mm_movie
WHERE movie_value * movie_qty >= 75
FOR UPDATE;
l_stk_flag mm_movie.stk_flag%TYPE; --> this
BEGIN
OPEN curr;
LOOP
FETCH curr INTO l_stk_flag;
EXIT WHEN curr%NOTFOUND;
UPDATE mm_movie
SET stk_flag = '*' --> this
WHERE CURRENT OF curr;
END LOOP;
COMMIT;
CLOSE curr;
END;
/

Why use a pl/sql anonymous block? Even if there is an "external" requirement for the functionality wrapped into pl/sql why use a cursor and loop? Using code that might be OK (from #Littlefoot) you retrieve a single column meeting your condition, iterate the resulting record set fetching that column but otherwise do nothing with it, and update a single row on every iteration of the loop with a literal value. SQL is designed for processing entire sets of rows at a time. Your processing can be done in a single update statement. Assuming there is an external requirement for a pl/sql block your code reduces to:
BEGIN
UPDATE mm_movie
SET stk_flag = '*'
WHERE movie_value * movie_qty >= 75;
COMMIT;
END;
Take away: When working with SQL stop thinking in terms of iterating (loops). Instead think of the commonality (set) of all objects to be processed. A different way of looking at problems and their corresponding solutions to be sure. Getting used to thinking that way will take some time, but in the long run your SQL and procedures will greatly improve because of it. Both in performance and clarity.

Related

Stored Procedure Error (column ambiguously defined)

Getting the following error for this line Create a stored procedure that takes as its argument as industryid
CREATE OR REPLACE PROCEDURE businesses (industryid INDUSTRY.INDUSTRYID%TYPE) AS
CURSOR businessloop IS
select tx.UNIONTITLE,b.ABNNUMBER,b.BUSINESSNAME
from business b INNER JOIN business_industry bi
on
b.ABNNUMBER = bi.ABNNUMBER
INNER JOIN industry ind
on
bi.INDUSTRYID = ind.INDUSTRYID
INNER JOIN trade_union tx
on
ind.UNIONID = tx.UNIONID
where
INDUSTRY.INDUSTRYID = industryid;
BEGIN
FOR ptr IN businessloop LOOP
DBMS_OUTPUT.PUT_LINE(ptr.UNIONTITLE);
DBMS_OUTPUT.PUT_LINE(ptr.ABNNUMBER);
DBMS_OUTPUT.PUT_LINE(ptr.BUSINESSNAME);
DBMS_OUTPUT.PUT_LINE('---------------------------');
END LOOP;
end businesses;
/
Execute businesses(6);
my output looks like this
Errors: PROCEDURE BUSINESSES
Line/Col: 5/1 PL/SQL: SQL Statement ignored
Line/Col: 16/23 PL/SQL: ORA-00918: column ambiguously defined
Line/Col: 26/1 PL/SQL: Statement ignored
Line/Col: 26/22 PLS-00364: loop index variable 'PTR' use is invalid
Line/Col: 27/1 PL/SQL: Statement ignored
Line/Col: 27/22 PLS-00364: loop index variable 'PTR' use is invalid
Line/Col: 28/1 PL/SQL: Statement ignored
Line/Col: 28/22 PLS-00364: loop index variable 'PTR' use is invalid
ORA-06550: line 1, column 7:
PLS-00905: object SQL_PQETEGYQDKCHNVXHUMIWRPLSE.BUSINESSES is invalid
I think error comes with this line
INDUSTRY.INDUSTRYID = industryid;
Answered in comments:
I think error comes with this line You're probably right. Rename procedure's parameter and eliminate names interference. – Akina 2 hours ago
thanks, man that's the reason, #Akina – sandun wijerathne Jerry 2 hours ago
Answer
CREATE OR REPLACE PROCEDURE businesses (industryidx INDUSTRY.INDUSTRYID%TYPE) AS
CURSOR businessloop IS
select tx.UNIONTITLE,b.ABNNUMBER,b.BUSINESSNAME
from business b INNER JOIN business_industry bi
on
b.ABNNUMBER = bi.ABNNUMBER
INNER JOIN industry ind
on
bi.INDUSTRYID = ind.INDUSTRYID
INNER JOIN trade_union tx
on
ind.UNIONID = tx.UNIONID
where
ind.INDUSTRYID = industryidx;
BEGIN
FOR ptr IN businessloop LOOP
DBMS_OUTPUT.PUT_LINE(ptr.UNIONTITLE);
DBMS_OUTPUT.PUT_LINE(ptr.ABNNUMBER);
DBMS_OUTPUT.PUT_LINE(ptr.BUSINESSNAME);
DBMS_OUTPUT.PUT_LINE('---------------------------');
END LOOP;
end businesses;
/
Execute businesses(6);

Trying to insert into a table while looping through pl SQL

I have a view that I need to loop through with all the inventory ids from another table. I keep receiving the following error: Error report -
ORA-06550: line 31, column 31:
PLS-00357: Table,View Or Sequence reference 'MISSINGINVENTORY' not allowed in this context
ORA-06550: line 31, column 2:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This is my current code I am not sure what the problem would be? I ma fairly new to pl sql
DECLARE
CURSOR inventory_ids_c IS
SELECT DISTINCT fzrfbth_status status,
fzbfbth_inventory_id id
FROM fzbfbth#develop_sw,
fzrfbth#develop_sw
WHERE fzbfbth_inventory_id = fzrfbth_inventory_id
ORDER BY fzrfbth_status,
fzbfbth_inventory_id;
inventory_ids_rec inventory_ids_c%ROWTYPE;
BEGIN
OPEN inventory_ids_c;
FETCH inventory_ids_c INTO inventory_ids_rec;
LOOP
EXIT WHEN inventory_ids_c%NOTFOUND;
fzkfims.P_set_inventory_id#develop_sw(inventory_ids_rec.id);
DECLARE
CURSOR inventory_items IS
SELECT *
FROM fzvfims#develop_sw;
BEGIN
OPEN inventory_items;
LOOP
FETCH inventory_items INTO Missinginventory(last_inventory_date,
atype_title
,
owner, orgn_code, orgn_title, locn_code, room, bldg, sort_room,
ptag
,
manufacturer, model, serial_num, description, custodian, po,
acq_date,
amount,
ownership, schev_year, tag_type, inventory_id, condition,
asset_type
);
EXIT WHEN inventory_items%NOTFOUND;
END LOOP;
CLOSE inventory_items;
END;
END LOOP;
END;
It seems that you are trying to fetch from cursor directly into Missinginventory table (if it is a table). Well, that won't work. First fetch into cursor variables, then insert those variables into a table.
I don't know what this:
fzkfims.P_set_inventory_id#develop_sw(inventory_ids_rec.id);
does; looks like some procedure call. I guess it sets some kind of an "environment", but I don't know how it reflects to code you wrote.
Anyway: why do you use cursor loops? Can't you directly insert data into the table? That would be faster and - hopefully - simpler.
Also, if you declare something, do that at the same place, at the beginning of that PL/SQL procedure. If you scatter declarations all over your code, it is difficult to maintain it. Furthermore, see whether cursor FOR loops are an option as they are simpler to use - Oracle does a lot of things for you. How? Although you still have to write cursor's SELECT statement, you don't have to declare cursor variables, open cursor, fetch, take care about exiting the loop, close the cursor. I'll try to rewrite your code, have a look (as you can see, no declare section at all!).
begin
for cur_1 in (select distinct fzrfbth_status status,
fzbfbth_inventory_id id
from fzbfbth#develop_sw
join fzrfbth#develop_sw
on fzbfbth_inventory_id = fzrfbth_inventory_id
order by fzrfbth_status,
fzbfbth_inventory_id
)
loop
fzkfims.p_set_inventory_id#develop_sw(cur_1.id);
for cur_2 in (select *
from fzvfims#develop_sw
)
loop
-- I shortened it to just a few columns
insert into missinginventory
(last_inventory_date,
atype_title,
asset_type)
values (cur_2.last_inventory_date,
cur_2.atype_title,
cur_2.asset_type);
end loop;
end loop;
end;

Apex button and procedure

Im trying to create button in Apex which execute given procedure. As input values I gave two date fields called Poczatek and Koniec. The button should execute this procedure on submit. It perfectly works in Oracle, but throw a lot of errors in Apex.
set serveroutput on
create or replace procedure KORELACJA(:Poczatek, :Koniec)
IS
miasto VARCHAR(25);
korelacja NUMBER;
cursor c1 is
SELECT TEMP.nazwa, corr(TEMP.temperatura, WILGOTNOSC.wilg)
FROM TEMP INNER JOIN WILGOTNOSC
on TEMP.nazwa = WILGOTNOSC.nazwa
and TEMP.data = WILGOTNOSC.data
WHERE TEMP.data between to_date(:Poczatek, 'YYYY-MM-DD') and to_date(:Koniec, 'YYYY-MM-DD')
GROUP BY TEMP.nazwa;
BEGIN
DBMS_OUTPUT.put_line(RPAD('Miasto',10)||RPAD('Korelacja',10));
open c1;
FOR i IN 1..6
LOOP
commit;
fetch c1 into miasto, korelacja;
DBMS_OUTPUT.put_line(RPAD(miasto,10)||RPAD(korelacja,10));
END LOOP;
close c1;
END KORELACJA;
/
Errors look like this:
1 error has occurred
ORA-06550: line 2, column 5: PL/SQL: ORA-00922: missing or invalid option
ORA-06550: line 2, column 1: PL/SQL: SQL Statement ignored ORA-06550: line 6,
column 11: PLS-00103: Encountered the symbol "NUMBER" when expecting one of the
following: := . ( # % ; ORA-06550: line 9, column 18: PLS-00103: Encountered the symbol "JOIN" when
expecting one of the following: , ; for group having intersect minus order start
union where connect
Anyone knows the solution?
I'd suggest you to leave the procedure in the database; call it from Apex.
As you said that it works OK, I'm not going to examine the code. Just modify the first line:
create or replace procedure KORELACJA(par_Poczatek in date,
par_Koniec in date)
is ...
Then, in Apex process, call the procedure as
korelacja(:p1_poczatek, :p2_koniec);
Note that you might need to apply TO_DATE function to those items, using appropriate format mask, such as
korelacja(to_date(:p1_poczatek, 'dd.mm.yyyy',
to_date(:p1_koniec , 'dd.mm.yyyy');
If you insist on keeping the procedure in Apex' process (I wouldn't recommend it), the you don't need CREATE PROCEDURE but an anonymous PL/SQL block. It won't accept any parameters - use Apex items directly.
declare
miasto VARCHAR(25);
korelacja NUMBER;
cursor ...
WHERE TEMP.data between to_date(:p1_Poczatek, 'YYYY-MM-DD') ...
begin
...
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');

Loop for inserting range of values in plsql

I am trying to run following sql query to populate table, but am unable to run it... any ideas?
BEGIN
FOR n in 21500..21600 LOOP
if mod(n,2)=0
then
dbms_output.put_line(n||'even');
else
insert into port_mapping(APP,VERSION,BUSINESS,CRITICAL,SUPPORTED,TYPE,PORT,NOTE)
values("SDS","1","No","Yes","Yes","Server",n,"NA");
end if;
END LOOP;
COMMIT;
END;
I am getting the error
PL/SQL: ORA-00984: column not allowed here ORA-06550: line 8, column
7: PL/SQL: SQL Statement ignored
Your values are in double-quotes, which makes them (column) identifiers as far as Oracle is concerned. You need single quotes:
...
values('SDS','1','No','Yes','Yes','Server',n,'NA');
...
SQL Fiddle.
You don't need PL/SQL for this - you could use a simpler insert-into with a select-connect-by - but I'm not sure if you're doing this as an exercise.
insert into port_mapping (APP,VERSION,BUSINESS,CRITICAL,SUPPORTED,TYPE,
PORT,NOTE)
select 'SDS','1','No','Yes','Yes','Server',
21500 + (level * 2) - 1,'NA'
from dual
connect by level < 51;
SQL Fiddle.

Resources