I have the following piece of code which is returning PLS-00222. So I want to compare the dates of an "old" id with a "new" id retrieved from cur_c1.
Here is the cursor where cur_table records come from:
CURSOR table_cur IS
SELECT
NEW_ID,
OLD_ID
FROM
TABLE_C
WHERE
C_ID = in_parameter_id; --This is input for the procedure
CURSOR cur_c1 (c_in_id NUMBER) IS
SELECT
FIELD_DATE
FROM
TABLE_D
WHERE
FIELD_ID = c_in_id;
FOR cur_table IN table_cur LOOP
...stuff...;
FOR c_cur IN cur_c1(cur_table.NEW_ID) LOOP
IF c_cur.field_date > cur_c1(cur_table.OLD_ID).field_date
THEN
v_exist := 'Y';
END IF;
END LOOP;
END LOOP;
How can I achieve my desired result?
Error:
2593/56 PLS-00222: no function with name 'cur_c1' exists in this scope
2593/17 PL/SQL: Statement ignored
I suggest that using cursors here is inefficient. Instead I suggest the following:
FOR cur_table IN table_cur LOOP
...stuff...;
SELECT d_old.FIELD_DATE,
d_new.FIELD_DATE
INTO dtOld_field_date,
dtNew_field_date
FROM DUAL
LEFT OUTER JOIN TABLE_D d_old
ON d_old.FIELD_ID = cur_table.OLD_ID
LEFT OUTER JOIN TABLE_D d_new
ON d_new.FIELD_ID = cur_table.NEW_ID;
IF dtNew_field_date > dtOld_field_date THEN
v_exist := 'Y';
END IF;
END LOOP;
Best of luck.
Related
I'm trying to use the rownum to simulate a column autonumbered as I need to use it as an ID. Since it is an ID, I look at the final table if no record with MAX (ID).
The problem I have is when I want to do arithmetic operations within the cursor or when you invoke, or when you want to use a function. The ROWNUM (v_id) field is empty me when I want to print with DBMS_OUTPUT . Anyone have any idea how to solve it without using sequences ?
Here put the sample code.
declare
max_id number;
CURSOR INSRT(w_max number) IS
SELECT f_max_fact_sap(to_number(V_ID),w_max) AS V_ID,Seriei,serief
FROM (SELECT To_Char(ROWNUM) AS V_ID, A.*
FROM (SELECT DISTINCT a.matnr, a.seriei, a.serief,a.xblnr,a.fecha_sap, ((SERIEF-SERIEI)+1) AS rango
FROM SOPFUN.TT_ZMOVIMI_FACTURADAS a
WHERE 0 =(SELECT COUNT(1)
FROM PA_ZMOVIMI_FACTURADAS B
WHERE A.SERIEI = B.SERIEI
AND A.SERIEF = B.SERIEF
AND A.MATNR = B.MATNR
AND A.FECHA_SAP=B.FECHA_SAP)
AND A.FECHA_SAP IS NOT NULL) A);
TYPE T_INSRT IS TABLE OF INSRT%ROWTYPE INDEX BY PLS_INTEGER;
V_INSRT T_INSRT;
begin
SELECT Max(Nvl(ID,10000)) INTO MAX_ID-- To Proof because the table is empty
FROM PA_ZMOVIMI_FACTURADAS;
OPEN INSRT(MAX_ID);
LOOP
FETCH INSRT BULK COLLECT INTO V_INSRT LIMIT 1000;
FOR I IN 1 .. V_INSRT.Count loop
DBMS_OUTPUT.PUT_LINE('ID: ' ||V_INSRT(I).V_ID||' SI: '||V_INSRT(I).SERIEI||' SI: '||V_INSRT(I).SERIEF||' OPERACION: '||to_char(f_max_fact_sap(V_INSRT(I).V_ID,MAX_ID)));
end loop;
EXIT WHEN INSRT%NOTFOUND;
END LOOP;
end;
I created a Cursor for a procedure. I am trying to apply a flag to records in that cursor.
Create or Replace Procedure Pledges
(IDdonor In Int)
is
Cursor Cur_Pledges is
Select dd_pledge.iddonor, dd_status.idstatus from dd_donor
join dd_pledge on dd_donor.iddonor=dd_pledge.iddonor
join dd_status on dd_pledge.idstatus=dd_status.idstatus;
Type All_Pledges2 is record(iddonor dd_pledge.iddonor%type, idstatus dd_status.idstatus%type, flag Varchar2(10));
Begin
For Rec_Pledges in Cur_Pledges LOOP
if rec_pledges.idstatus = '10' THEN Flag := 'True';
elsif rec_pledges.idstatus= '20' THEN Flag := 'False';
End if;
Insert Into All_Pledges
Values(rec_pledges.idddonor, rec_pledges.idstatus, flag);
End Loop;
End;
You are wrongly using the type record variable, I have made the changes please check below, this will work:
Create or Replace Procedure Pledges
(IDdonor In Int)
is
Cursor Cur_Pledges is
Select dd_pledge.iddonor, dd_status.idstatus from dd_donor
join dd_pledge on dd_donor.iddonor=dd_pledge.iddonor
join dd_status on dd_pledge.idstatus=dd_status.idstatus;
Type All_Pledges2 is record(iddonor dd_pledge.iddonor%type, idstatus dd_status.idstatus%type, flag Varchar2(10));
-- new change below
allpledges2 All_Pledges2;
Begin
For Rec_Pledges in Cur_Pledges LOOP
if rec_pledges.idstatus = '10' THEN
allpledges2.Flag := 'True';
elsif rec_pledges.idstatus= '20' THEN
allpledges2.Flag := 'False';
End if;
Insert Into All_Pledges
Values(rec_pledges.iddonor, rec_pledges.idstatus, allpledges2.flag);
End Loop;
End;
While you can do this with a cursor loop, you shouldn't. Generally, PL/SQL performs best if you minimize the number of context changes and let the SQL optimizer do it's job. This procedure should consist of a single insert statement:
CREATE OR REPLACE PROCEDURE pledges (iddonor IN INT) IS
BEGIN
INSERT INTO all_pledges
SELECT dd_pledge.iddonor,
dd_status.idstatus,
CASE dd_status.idstatus
WHEN '10' THEN 'True'
WHEN '20' THEN 'False'
END
FROM dd_donor
JOIN dd_pledge ON dd_donor.iddonor = dd_pledge.iddonor
JOIN dd_status ON dd_pledge.idstatus = dd_status.idstatus;
END pledges;
It probably worth noting that, as written, the iddonor parameter is superfluous: since you aren't referencing it in the code, it serves no purpose.
If the goal is to simply return the results to another program, you don't want an insert at all (using a table to return a result set is not a good pattern in Oracle). The best way to handle that is typically to open a ref cursor and return that:
CREATE OR REPLACE PROCEDURE pledges (iddonor IN INT,
all_pledges OUT SYS_REFCURSOR) IS
BEGIN
OPEN all_pledges FOR
SELECT dd_pledge.iddonor,
dd_status.idstatus,
CASE dd_status.idstatus
WHEN '10' THEN 'True'
WHEN '20' THEN 'False'
END
FROM dd_donor
JOIN dd_pledge ON dd_donor.iddonor = dd_pledge.iddonor
JOIN dd_status ON dd_pledge.idstatus = dd_status.idstatus
WHERE dd_donor.iddonor = pledges.iddonor;
END pledges;
Alternately, you could return a user-defined type or write the results to a global temporary table.
I have created a cursor which returns me a set of rows. While iterating through each of the row, I want to get another result set (by forming a SELECT statement by with a WHERE clause having value from the processed row) from another table. I am a newbie in PLSQL. Can you please guide me on how this could be done? (Can we have a Cursor defined inside the loop while looping for the resultset of the cursor)?
Please excuse me if I am not able to make myself clear.
Thanks in advance
DECLARE
CURSOR receipts IS
SELECT CREATED_T, ACCT_NO, AMT FROM receipt_t
WHERE OBJ_TYPE='misc';
receipts_rec receipts%ROWTYPE;
BEGIN
-- Open the cursor for processing
IF NOT receipts%ISOPEN THEN
OPEN receipts;
END IF;
LOOP
FETCH receipts INTO receipts_rec;
EXIT WHEN receipts%NOTFOUND;
/* Loop through each of row and get the result set from another table */
newQuery := 'SELECT * FROM ageing_data WHERE ACCT_NO = ' || receipts_rec.ACCT_NO;
-- Execute the above query and get the result set, say RS
LOOP
-- For above result set-RS
END LOOP;
END LOOP;
CLOSE receipts;
END;
Yes, you can define a cursor that takes a set of parameters and use those values in the WHERE clause.
DECLARE
CURSOR c_cursor1 IS
SELECT field1, field2, ... , fieldN
FROM table1
WHERE conditions;
CURSOR c_cursor2 (p_parameter NUMBER) IS
SELECT field1, field2, ..., fieldN
FROM table2
WHERE table2.field1 = p_parameter;
BEGIN
FOR record1 IN c_cursor1 LOOP
FOR record2 IN c_cursor2(record1.field1) LOOP
dbms_output.put_line('cursor 2: ' || record2.field1);
END LOOP
END LOOP;
END;
Yes, you can do that, but there is absolutely no reason to. Try the following:
BEGIN
FOR aRow IN (SELECT rt.CREATED_T, rt.ACCT_NO, rt.AMT, ad.*
FROM RECEIPT_T rt
INNER JOIN AGEING_DATA ad
ON (ad.ACCT_NO = rt.ACCT_NO)
WHERE rt.OBJ_TYPE='misc')
LOOP
-- Process the data in aRow here
END LOOP;
END;
This does exactly the same work as the original "loop-in-a-loop" structure but uses the database to join the tables together on the common criteria instead of opening and closing cursors multiple times.
Share and enjoy.
Something like this can be done in the following manner:
DECLARE
CURSOR cursor1 IS
SELECT *
FROM table1;
CURSOR cursor2 IS
SELECT *
FROM table2
WHERE column1 = I_input_param;
BEGIN
FOR table_1_rec in cursor1 LOOP
I_input_param := table_1_rec.column_1;
FOR table_2_rec in cursor2 LOOP
....
....
END LOOP;
END LOOP;
END;
I have used an implicit open/fetch here. I hope you get the idea.
declare
cursor lc is
select *
from (select a.lin, a.pr,
b.name, sum(a.up) as u,
sum (a.d) as d
from li_dy_4 a,
p_list b
where a.pr=b.id
and b.parent_id != 0
and a.partitionid <= 308
and a.partitionid >= 302
and a.pr in (91,80)
GROUP BY a.pr, b.name, a.lin
order by d desc) ;
rec lc%ROWTYPE;
BEGIN
open lc;
loop
FETCH lc into rec;
dbms_output.put_line(rec.pr);
exit when lc%NOTFOUND;
end loop;
close lc;
END;
the above statement works fine for me. What I am not capable of finding anything hopeful is changing the value after the "in" statement which is a.pr in (91,80)
I have listed the values here manually, but I want to pass it to the cursor as an array of numbers for a.pr column. In short I want to do a.pr = idlist wher idlist is an array. Please anyone tell me if my idea is possible.
Just want to remind you, the IN clause supports 1000 items only. And that could be the primary reason ,there's nothing called BULK BINDING for SELECT Queries. We have FORALL INSERT/UPDATE, which is like BULK BINDING. Unfortunately select has none.
But still you can achieve your requirement in a different fashion.
You can try a global temporary table(GTT) which a temporary table with "scope of the data inserted" is only to that session.
You can FORALL INSERT all your data for IN clause into that table, and join the TABLE to your Query.
Else you can have a nested table (if oracle 10g) or a simple pl/sql type itself (if oracle 11g), with all your IN class items as records and join it to your Query.
Example: Using NESTED TABLE , effective for less number(<10000) of items
CREATE TYPE pr AS OBJECT
(pr NUMBER);
/
CREATE TYPE prList AS TABLE OF pr;
/
declare
myPrList prList := prList ();
cursor lc is
select *
from (select a.lin, a.pr,
b.name, sum(a.up) as u,
sum (a.d) as d
from li_dy_4 a,
p_list b,
TABLE(CAST(myPrList as prList)) my_list
where a.pr=b.id
and b.parent_id != 0
and a.partitionid <= 308
and a.partitionid >= 302
and a.pr = my_list.pr
GROUP BY a.pr, b.name, a.lin
order by d desc) ;
rec lc%ROWTYPE;
BEGIN
/*Populate the Nested Table, with whatever collection you have */
myPrList := prList ( pr(91),
pr(80));
/*
Sample code: for populating from your TABLE OF NUMBER type
FOR I IN 1..your_input_array.COUNT
LOOP
myPrList.EXTEND;
myPrList(I) := pr(your_input_array(I));
END LOOP;
*/
open lc;
loop
FETCH lc into rec;
exit when lc%NOTFOUND; -- Your Exit WHEN condition should be checked afte FETCH iyself!
dbms_output.put_line(rec.pr);
end loop;
close lc;
END;
/
I don't know the exact structure of your global table but you can use the collection in cursor like this
declare
cursor c1 is
select last_name ls from empc;
type x is table of employees.last_name%type;
x1 x := x();
cnt integer :=0;
begin
for z in c1 loop
cnt := cnt +1;
x1.extend;
x1(cnt) := z.ls;
if x1(cnt) is NULL then-----------
DBMS_OUTPUT.PUT_LINE('ASHISH');
end if;
dbms_output.put_line(cnt || ' '|| x1(cnt));
end loop;
end;
I have two nested cursors. I mean one cursor Is Inside of another one.
I want to use a field of outer cursor inside the inner one. something like this:
Inner_cursor.outer_cursor.outer_cursor_column;
but It does not work even I use like this:
Inner_cursor.(outer_cursor.outer_cursor_column);
Is there any way I could do this?
EDIT:
This Is My Code:
CREATE OR REPLACE PROCEDURE TEST1
AS
CURSOR loop_relation IS
SELECT * FROM RELATION_table;
relation_rec loop_relation%rowtype;
CURSOR loop_BIG_TABLE IS
SELECT * FROM BIG_TABLE;
BIG_TABLE_rec loop_BIG_TABLE%rowtype;
BEGIN
FOR RELATION_REC IN LOOP_RELATION
LOOP
FOR BIG_TABLE_rec in loop_BIG_TABLE
LOOP
IF (BIG_TABLE_REC.RELATION_REC.DESTINATION_PK IS NULL) THEN
UPDATE BIG_TABLE
SET BIG_TABLE.RELATION_REC.DESTINATION_PK = (
SELECT RELATION_REC.SOURCE_FK FROM RELATION_REC.SOURCE_TABLE
WHERE RELATION_REC.SOURCE_PK = BIG_TABLE_REC.RELATION_REC.SOURCE_PK)
WHERE BIG_TABLE_REC.ID = BIG_TABLE.ID;
END IF;
END LOOP;
END LOOP;
END TEST1;
/
my problem is in the lines that i use three dot(.) to use a value of outer cursor in inner cursor.
Here's an example of two nested cursors and variables from the outer one used in the inner one. I hope it helps you.
BEGIN
FOR r_outer in (
select tab1.field1
from table1 tab1 )
LOOP
FOR r_inner in (
select tab2.field2
from table2 tab2
where tab2.field2 = r_outer.field1 )
LOOP
dbms_output.put_line(r_outer.field1);
dbms_output.put_line(r_inner.field2);
END LOOP;
END LOOP;
END;
For reference, I created a procedure to display how to use outer cursor value inside inner cursor value. I hope this resolves your query.
CREATE OR REPLACE PROCEDURE cur_inside_cur(my_cur OUT sys_refcursor)
AS
CURSOR roy_cur IS
SELECT name FROM avrajit;
roy_cur1 roy_cur%ROWTYPE;
BEGIN
OPEN roy_cur;
LOOP
FETCH roy_cur INTO roy_cur1;
EXIT WHEN roy_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(roy_cur1.name);
OPEN my_cur FOR
SELECT department FROM avrajit
WHERE name=roy_cur1.name;
END LOOP;
END cur_inside_cur;
OUTPUT
var c refcursor;
begin
cur_inside_cur(:c);
end;
print c;