Home » SQL & PL/SQL » SQL & PL/SQL » Cursor Query help (Oracle)
Cursor Query help [message #641121] |
Mon, 10 August 2015 18:23 |
|
GeetikaLearn
Messages: 3 Registered: August 2015
|
Junior Member |
|
|
Hi All,
I am new bee to Oracle Cursors.
I have requirement like below.
we have two tables one summarytable and Detailtable ( will have more number of records compare summary table records)
based on summary table catno and quaterdate we need to fetch those corresponding records in detail table in random and need to update country column in detailtable (which is a empty column) with country column with summary table
example: at MHP-300 catno,7/1/2004 QuarterDate we have 15 QtyToAllocate,so from detailtable we need to fetch 15 records and update county column to Belgium and in next iteration
MHP-300 catno, 7/1/2004 we have 5 QtyToAllocate,so from detailtable we need to fetch next 5 records ( means after 15th row ( all 15 rows which we allocated to Belgium)) and update country column to Chile
Summarytable
CatNo Country QuarterDate QtyToAllocate
MHP-300 Belgium 7/1/2004 15
MHP-300 Bulgaria 4/1/2004 7
MHP-300 Bulgaria 7/1/2004 6
MHP-300 Chile 4/1/2004 8
MHP-300 Chile 7/1/2004 5
MHP-300 China 4/1/2004 2
MHP-300 China 7/1/2004 2
MHP-300 Cyprus 4/1/2004 4
MHP-300 Cyprus 7/1/2004 4
MHP-300 France 4/1/2004 2
MHP-300 France 7/1/2004 28
Detail table
ATALOG_NO, QUARTERDATE, ALLOCATEDCOUNTRY ( empty colun),ALLOCATED (empty column)
MHP-300 7/1/2004 -- more than 15 records in detail table need to update only 15 records for belgium rest of records empty
MHP-300 4/1/2004 -- more than 7 records in detail table
MHP-300 7/1/2004 -- more than 6 records in detail table
MHP-300 4/1/2004 -- more than 8 records in detail table
MHP-300 7/1/2004 -- more than 5 records in detail table
MHP-300 4/1/2004 -- more than 2 records in detail table
MHP-300 7/1/2004 -- more than 2 records in detail table
MHP-300 4/1/2004 -- more than 4 records in detail table
MHP-300 7/1/2004 -- more than 4 records in detail table
MHP-300 4/1/2004 -- more than 2 records in detail table
MHP-300 7/1/2004 -- more than 28 records in detail table
I have written below code receiving error message like "c1 must be declared with for update'.
Please let me know what mistake i made and help me how to resolve it.
declare
cursor c_WHOLECATNO is
select * from Summarytable where CATNO= 'MHP-300' and QUARTERDATE =TO_DATE('04/01/2004 00:00:00', 'MM/DD/YYYY HH24:MI:SS' ) and Country='Belgium' ;
cursor c1(v_CatNo in varchar2, v_QUARTERDATE in date, v_QtyToAllocate in number,v_Country in varchar2) is
( select * from ( SELECT cdt.CATALOG_NO, cdt.QUARTERDATE, cdt. ALLOCATEDCOUNTRY,cdt.ALLOCATED, ROW_NUMBER () OVER (PARTITION BY cdt.CATALOG_NO, cdt.QUARTERDATE ORDER BY cdt.CATALOG_NO, cdt.QUARTERDATE) seq_no
FROM detailtable cdt where cdt. ALLOCATEDCOUNTRY is null ) where CATALOG_NO=v_CatNo and QUARTERDATE=v_QUARTERDATE and seq_no<= v_QtyToAllocate );
begin
for WHOLECATNO_rec in c_WHOLECATNO loop
for rec in c1(WHOLECATNO_rec.CatNo, WHOLECATNO_rec.QUARTERDATE,WHOLECATNO_rec.QtyToAllocate,WHOLECATNO_rec.Country) loop
update detailtable set ALLOCATED ='Y',ALLOCATEDCOUNTRY=WHOLECATNO_rec.country
-- where rowid=rec.rowid ;
where current of c1;
end loop;
end loop;
end;
/
Thank you
|
|
|
|
Re: Cursor Query help [message #641143 is a reply to message #641123] |
Tue, 11 August 2015 03:28 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
The error message means exactly what it says.
If you want to use WHERE CURRENT OF then the cursor concerned needs a FOR UPDATE clause on it. Check the docs if you're not sure where to put it.
That said, there's zero need for cursors here. Should be a single update statement.
|
|
|
Re: Cursor Query help [message #641196 is a reply to message #641143] |
Tue, 11 August 2015 13:31 |
|
GeetikaLearn
Messages: 3 Registered: August 2015
|
Junior Member |
|
|
Thank you for the links. I have formatted my{code}.
Here is my code,I am receiving this error message "ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc".
I need row_number() partition logic, how can I resolve FOR UPDATE issue with out creating view.
Thanks for help.
DECLARE
CURSOR c_wholecatno IS
SELECT *
FROM summarytable
WHERE catno= 'MHP-300'
AND quarterdate =to_date('04/01/2004 00:00:00', 'MM/DD/YYYY HH24:MI:SS' )
AND country='Belgium' ;
CURSOR c1(v_catno IN VARCHAR2, v_quarterdate IN DATE, v_qtytoallocate IN NUMBER,v_country IN VARCHAR2)
IS
(
SELECT *
FROM (
SELECT cdt.catalog_no,
cdt.quarterdate,
cdt. allocatedcountry,
cdt.allocated,
row_number () over (PARTITION BY cdt.catalog_no, cdt.quarterdate ORDER BY cdt.catalog_no, cdt.quarterdate) seq_no
FROM detailtable cdt
WHERE cdt. allocatedcountry IS NULL )
WHERE catalog_no=v_catno
AND quarterdate=v_quarterdate
AND seq_no<= v_qtytoallocate ) FOR UPDATE;
BEGIN
FOR wholecatno_rec IN c_wholecatno
LOOP
FOR rec IN c1(wholecatno_rec.catno, wholecatno_rec.quarterdate,wholecatno_rec.qtytoallocate,wholecatno_rec.country)
LOOP
UPDATE detailtable
SET allocated ='Y',
allocatedcountry=wholecatno_rec.country
WHERE CURRENT OF c1;
END LOOP;
END LOOP;
END;
|
|
|
|
Re: Cursor Query help [message #641239 is a reply to message #641197] |
Wed, 12 August 2015 04:46 |
|
Littlefoot
Messages: 21808 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
One option is to omit FOR UPDATE (line 31) and WHERE CURRENT OF clause (line 42) and use WHERE condition (lines 43, 44) which will tell Oracle what records to update. Something like this:
SQL> DECLARE
2 CURSOR c_wholecatno
3 IS
4 SELECT *
5 FROM summarytable
6 WHERE catno = 'MHP-300'
7 AND quarterdate =
8 TO_DATE ('07/01/2004 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
9 AND country = 'Belgium';
10
11 CURSOR c1 (
12 v_catno IN VARCHAR2,
13 v_quarterdate IN DATE,
14 v_qtytoallocate IN NUMBER,
15 v_country IN VARCHAR2)
16 IS
17 (SELECT *
18 FROM (SELECT cdt.catalog_no,
19 cdt.quarterdate,
20 cdt.allocatedcountry,
21 cdt.allocated,
22 ROW_NUMBER ()
23 OVER (PARTITION BY cdt.catalog_no, cdt.quarterdate
24 ORDER BY cdt.catalog_no, cdt.quarterdate)
25 seq_no
26 FROM detailtable cdt
27 WHERE cdt.allocatedcountry IS NULL)
28 WHERE catalog_no = v_catno
29 AND quarterdate = v_quarterdate
30 AND seq_no <= v_qtytoallocate);
31 -- FOR UPDATE;
32 BEGIN
33 FOR wholecatno_rec IN c_wholecatno
34 LOOP
35 FOR rec IN c1 (wholecatno_rec.catno,
36 wholecatno_rec.quarterdate,
37 wholecatno_rec.qtytoallocate,
38 wholecatno_rec.country)
39 LOOP
40 UPDATE detailtable
41 SET allocated = 'Y', allocatedcountry = wholecatno_rec.country
42 --WHERE CURRENT OF c1;
43 WHERE catalog_no = wholecatno_rec.catno
44 AND quarterdate = wholecatno_rec.quarterdate;
45 END LOOP;
46 END LOOP;
47 END;
48 /
PL/SQL procedure successfully completed.
SQL>
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 11:09:35 CDT 2024
|