Two cursors in one procedure
Hi All,
Two cursors in the same procedure will slows down the execution ?. Please can any one suggest one this.
I aggree with Sundar, you might be able to update this without using a cursor. My general understanding is to avoid using cursor as much as possible for performance reasons, if you are doing a lot of inserts/updates etc. If you are doing lot of updates and you must have to iterate through a loop due to some reason then try to use bulk processing (use bulk inserts, updates, one sql query to access data from database to avoid conext switching etc.).
Similar Messages
-
I am working on procedure, Please give ideas where i can develop this logic more meaning and easy
the logic is as follows
Table1 is having same Id's ABC having same seq number 100 as per legacy data (This is wrong entry in legacy data)
ID -- Seqnum
ABC -- 100
ABC -- 100
I created a table2 where it will have max seqnum from legacy for this ID's for example ABC
ID -- Max seqnum
ABC --500
In my procedure I will have two cursors
First cursor will be have Table1 detail
Second Cursor will have Table2 Details
In above ex the table will have the first record should be unchanged i.e.,
ABC -- 100 --100
but second record will change to
ABC -- 100 -- 501(This next value of the max seq number from table 2/Cursor 2)
I have to create third column which will hold the next seqnumber for that ID ABC
I should load both records changing the seqnumber so I will not have any duplicate records for same ID.
So final records will look like
ID Seq# Final_Seq#
ABC -- 100 -- 100
ABC -- 100 -- 501
Thanks in advance.Adjusted accordingly to the message above
with
data_tab as
(select 'ABC' id,100 seq from dual union all
select 'ABC',100 from dual union all
select 'ABC',100 from dual union all
select 'BCD',110 from dual union all
select 'CDE',120 from dual union all
select 'CDE',120 from dual union all
select 'DEF',130 from dual union all
select 'EFG',140 from dual union all
select 'EFG',140 from dual union all
select 'EFG',140 from dual union all
select 'EFG',240 from dual union all
select 'EFG',240 from dual union all
select 'FGH',150 from dual
max_nums as
(select 'ABC' id,200 max_num from dual union all
select 'BCD',300 from dual union all
select 'CDE',400 from dual union all
select 'DEF',500 from dual union all
-- select 'EFG',600 from dual union all
select 'FGH',700 from dual
select d.id,d.seq,row_number() over (partition by d.id,d.seq order by null) rn,m.max_num,
case when row_number() over (partition by d.id,d.seq order by null) = 1
then d.seq
else nvl(m.max_num,d.seq) + row_number() over (partition by d.id,d.seq order by null) - 1
end new_seq
from data_tab d,
max_nums m
where d.id = m.id(+)
ID
SEQ
RN
MAX_NUM
NEW_SEQ
ABC
100
1
200
100
ABC
100
2
200
201
ABC
100
3
200
202
BCD
110
1
300
110
CDE
120
1
400
120
CDE
120
2
400
401
DEF
130
1
500
130
EFG
140
1
140
EFG
140
2
141
EFG
140
3
142
EFG
240
1
240
EFG
240
2
241
FGH
150
1
700
150 -
Linking property nodes to two cursors in one graph
Hello,
I have got one graph with 2 cursors. And I need to by able to set the coordinates of each cursor by using property nodes. However, if I click right button mouse button - create - property node - cursor - cursor position - -then there are only 3 possibilities "All elements", "X", Y" which links only to one of the cursors (actually I don`t know which). How is possible to link 2 cursors`s position property nodes to 2 cursors in one graph?
Thank you a lot for discussion.
Martin Pekar
Solved!
Go to Solution.Also, if you have more than a few cursors, you can make the code more scalable by using an array of positions and a FOR loop as follows.
Now the code automatically adapts th the number of cursors you actually have.
(Of course all other common sense applies, for example only set this when the position changes. Don't write the same cursor postions with every iteration of the loop over and over. )
Message Edited by altenbach on 05-06-2009 06:25 AM
LabVIEW Champion . Do more with less code and in less time .
Attachments:
MulticursorII.png 4 KB -
Return multiple cursors from one procedure
Hi,
I have a stored procedure that is suposed to return multiple records. I know that for each table that I return I have to add a param to the stored procedure, param with ref cursor type.
I do not know exactly how many tables I have to return, it depends on data from some tables.
Is it possible to return an unknown number of tables? like an array or something ...
Please helpIn order to keep it transparent to the developer, I have to translate exactly the stored procedures from SQL Server to Oracle.Well "exactly" is not something that you are going to achieve. You're talking about two disperate languages. That's like saying you want to translate French to English, but it must stay French.
You obviously want to mimick the existing functionality, but in some cases you're just going to have to bite the bullet and accept that you won't be able to do it totally transparently. In this case, the developers are going to have to make some changes at their end of things too.
To translate the procedures from T-SQL to Oracle was
the easiest and most convenient option to the
developers, so they will not have to write different
code for Oracle and SQL.But they will. Anyone who evaluated the project and determined that the developers wouldn't need to do anything when moving from one platform to another should be sacked for incompetence.
;) -
Error while opening two cursors in same procedure...
Hi :
I have written following Procedure:
Declare
procedure pro_canefortnight_master Is
kr_caneperiodid integer;
last_caneperiod_id number(10,0);
kr_forthnightid integer;
last_forthnight_id number(10,0);
ref_caneperiodid integer;
ref_seasonyrid integer;
ctr integer:= 1;
CURSOR comp_cur IS select * from CMS_SEASON_YEAR_MASTER ;
comp_rec comp_cur%ROWTYPE;
BEGIN
OPEN comp_cur;
FETCH comp_cur INTO comp_rec;
WHILE comp_cur%FOUND
LOOP
kr_caneperiodid:= comp_rec.SEASON_YR_ID;
select max(crayom_db.cr_cane_calender.cr_cane_calender_ID) into last_caneperiod_id from crayom_db.cr_cane_calender;
if last_caneperiod_id <> 0 then
last_caneperiod_id:= last_caneperiod_id + 1;
else
last_caneperiod_id:= 1000000;
end if;
insert into crayom_db.CR_CANE_CALENDER(cr_cane_calender_ID,ad_client_id, ad_org_id, isactive, created, createdby, updated, updatedby,CR_CANE_CALENDER_NAME,DESCRIPTION) values(last_caneperiod_id,11,11,comp_rec.current_season,sysdate,100,sysdate,11,comp_rec.DESC_MA,comp_rec.DESC_EN);
COMMIT;
/*insert into crayom_db.CR_IdBackup(kr_cultivationtypeid,cr_cultivationtypeid) values(kr_cultivationtypeid, last_cultivationtype_id);*/
insert into crayom_db.temp(kr_bpartnerid,cr_bpartnerid) values(comp_rec.season_yr_id,last_caneperiod_id);
commit;
FETCH comp_cur INTO comp_rec;
End LOOP;
close comp_cur;
CURSOR comp_cur1 IS select * from CMS_FORTH_NIGHT_MASTER ;
comp_rec1 comp_cur1%ROWTYPE;
OPEN comp_cur1;
FETCH comp_cur1 INTO comp_rec1;
WHILE comp_cur1%FOUND
LOOP
kr_forthnightid:= comp_rec1.forthnight_id;
select max(crayom_db.cr_cane_calender_period.cr_cane_calender_period_ID) into last_forthnight_id from crayom_db.cr_cane_calender_period;
if last_forthnight_id <> 0 then
last_forthnight_id:= last_forthnight_id + 1;
else
last_forthnight_id:= 1000000;
end if;
if ref_seasonyrid <> comp_rec1.season_yr_id then
ref_seasonyrid:= comp_rec1.season_yr_id;
else
ctr:= ctr + 1;
end if;
SELECT cr_bpartnerid into ref_caneperiodid from crayom_db.temp where kr_bpartnerid = comp_rec1.season_yr_id;
dbms_output.put_line(ref_caneperiodid);
insert into crayom_db.CR_CANE_CALENDER_period(cr_cane_calender_period_ID,ad_client_id, ad_org_id, isactive, created, createdby, updated, updatedby,Enddate,name, startdate, cr_cane_calender_id) values(last_forthnight_id,11,11,comp_rec1.current_season,sysdate,100,sysdate,11,comp_rec1.end_date,'ForthNight' || ctr, comp_rec1.start_date,ref_caneperiodid);
commit;
FETCH comp_cur1 INTO comp_rec1;
End LOOP;
close comp_cur1;
END;
BEGIN
pro_canefortnight_master();
END;
But I am getting following Error:
Error report:
ORA-06550: line 429, column 8:
PLS-00103: Encountered the symbol "COMP_CUR1" when expecting one of the following:
:= . ( @ % ;
ORA-06550: line 433, column 1:
PLS-00103: Encountered the symbol "FETCH" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The symbol "begi
ORA-06550: line 472, column 1:
PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
ORA-06550: line 477, column 4:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Can any body help me?
Thank You.
Edited by: [email protected] on Oct 22, 2009 4:36 AM[email protected] wrote:
CURSOR comp_cur1 IS select * from CMS_FORTH_NIGHT_MASTER ;
comp_rec1 comp_cur1%ROWTYPE;This is probably the issue. All variable definitions, including cursors, must be in the DEFINE section of a procedure (between either DECLARE ... BEGIN or AS/IS ... BEGIN).
In addition I wanted to note the following:
1. When posting it is helpful to identify your Oracle version (e.g. 10.2.0.4).
2. Any code that is posted should be placed between \ tags to improve readability.
3. It looks like the code is doing "slow by slow" processing (a.k.a single record processing). It could probably be made much cleaner, maintainable and better performing by possibly using INSERT .. AS SELECT ... or something along those lines. -
I was trying to use two cursors in a procedure and while compiling in DBA Studio, I get the following error: "Line # = 19 Column # = 14 Error Text = PLS-00341: declaration of cursor 'RECORD_ID_CURSOR' is incomplete or malformed"
Are you not supposed to have two cursors in a procedure? Please help. Thanks.
PaulPLS-00341 declaration of cursor 'string' is incomplete or malformed
this is the error you are getting. You can have 2 cursors but the way it's defined is incorrect. Check your spellings and the way it's defined. -
"multiple cursor in a procedure" is possible?
Hi.. everyone.
Is it possible , "multiple cursor in one procedure"?
For example,
create or replace procedure xxx
is
vAAA varchar2(10);
vBBB varchar2(10);
cursor cur_1 is select .............................;
cursor cur_2 is select .............................;
begin
-- the first cursor
open cur_1;
loop
end loop;
close cur_1;
-- the second cursor
open cur_2;
loop
end loop;
close cur_2;
end;
If it is not possible, what is the way to use
multiple cursor(more than 2) in ONE procedure?
Thanks in advance.
Have a nice day.
Ho.no problem
SQL> drop procedure xxx;
Procedure dropped.
SQL> CREATE OR REPLACE
2 procedure xxx
3 is
4 vAAA varchar2(10);
5 vBBB varchar2(10);
6 cursor cur_1 is select employee_id from employees where rownum < 2;
7 cursor cur_2 is select department_id from departments where rownum < 2;
8
9 begin
10 -----------------------------------------------
11 -- the first cursor
12 -----------------------------------------------
13 dbms_output.put_line('Cursor 1 Result');
14 open cur_1;
15 loop
16 fetch cur_1 into vAAA;
17 exit when cur_1%notfound;
18 dbms_output.put_line(vAAA);
19 end loop;
20 close cur_1;
21 -----------------------------------------------
22 -- the second cursor
23 -----------------------------------------------
24 dbms_output.put_line('Cursor 2 Result');
25 open cur_2;
26 loop
27 fetch cur_2 into vBBB;
28 exit when cur_2%notfound;
29 dbms_output.put_line(vBBB);
30 end loop;
31 close cur_2;
32 -----------------------------------------------
33 end;
34 /
Procedure created.
SQL> execute xxx;
Cursor 1 Result
100
Cursor 2 Result
10
PL/SQL procedure successfully completed. -
Two cursor variable can match with each other
I have 2 cursor in one procedure
it is possible that cursor 1 variable do some calculation with cursor 2 variable.
for example cursor 1 is for inStock and cursor 2 for Out stock
Balance = Cursor1.inQTY - Cursor2.outQTYSuppose
Cursor Cursor1 is
select Idate,Icode,IFcode,Iqty,IPrice from returnreport;
Cursor Cursor2 is
select Rdate,Rcode,Rfcode,Rqty,Rprice from TempRed;Cursor1 result is
IDATE Icode Ifcode Iqty IPRICE REDUNITS
28/06/2010 13 1 148375.7308 101.8004
30/08/2010 13 1 100284.0398 101.7111 Cursor2 Result is
Rdate RCODE RFCODE Rqty Rprice
01/07/2010 13 1 2804.7464
31/08/2010 13 1 39315.8646
08/09/2010 13 1 24515.9089
28/09/2010 13 1 80000
30/09/2010 13 1 11693.4219 I want to update Cursor 1 table's Column REDUNIT with Cursor 2.
go_block('returnreport');
first_record;
check here IQTY of RETURNREPORT
Loop
Cursor1.Iqty - Cursor2.Rqty
it is like a first in first out basis
check balance = Iqty - Rqty
Update Redunits = Balance
if balance = 0 then
go 2nd row
and update remaining balance in 2ns row.
regards -
TWO CURRENCIES IN ONE Purchase Orders Release Procedure.
TWO CURRENCIES IN ONE Purchase Orders Release Procedure.
Please provide a solution if a release strategy is maintained for two currencies like USD and SGD for more clarification please Read the below Requirement.
Requirement for Singapore:
u2022 COMPANY Asia (Singapore) wants its own Purchase order release strategy is based on their local currency (SGD)
u2022 Whereas COMPANY is using only USD as currency in Purchase order release strategy.
u2022 Standard SAP works based on Character currency which is defining in Class of Purchase release strategy and converts with Local currency to compare with Character Currency.
Required analysis:
u2022 The value assigned in classification is based on Conversion from SGD to USD, Due to Exchange rate fluctuations the assigned value is differ by large amounts and hence system is either determining the higher release strategy or Lower release strategy
u2022 But during Release Strategy Configuration the Net order Value is assigned as 3600 USD considering the Exchange as 1.388889.
u2022 As per the exchange rate in the purchase order 1.3799 the system is calculation the PO Net order Value but the Strategy is Determined based on the value maintained according to exchange Rate 1.388889
In the Purchase Order the system is
Ex: Total Purchase Order Value is 4765.21 SGD and 3453.30 in USD
For Level 1 the value limit is 5000 SGD if it exceeds 5000 SGD it has to go to his manager (Level 2) Exchange rate on 01.04.2008 is 1.3799.
System Considerations:
Possible Solution:
1. Create a new Class with Characteristic with Currency Value SGD System should determine the class based on the combination of Plant 6000 and Document Category F Purchase Order.
2. By using Enhancement spot with Function Module ME_RELEASE_STRATEGIE/ ME_RELEASE_STRATEGIE_EKKO
Logic:
1. System will determine the class PUR_RELEASE from table T16FG if the system finds Plant as 6000 and Document Category as F Then replace PUR_RELEASE with New class as ZPUR _6000.
2. Maintain Z table for the combination of Plant and Class for future requirement
Edited by: Mohammad Irfan on Jun 8, 2009 10:41 AMHi Mohamed,
You can create separate characteristics far all check points and assign those to class.
Regards,
Prasath -
Hi,
I am using 10g and wanted to check if we can use a dynamic cursor in a procedure.
Following is my code and wanted to see if that can work with every query passed as a parameter.
example ,
exec test1 ('select col1, col2, col3 from table1','Two columns Sql')
exec test1 ('select col1 from table2','one columns Sql')
exec test1 ('select col1, col2, col3, col4, col5 from table3','Five columns Sql')
CREATE OR REPLACE procedure test1 (p_sql IN VARCHAR2, p_subject IN VARCHAR2
is
v_cu_string VARCHAR2(2000);
v_string VARCHAR2(2000);
v_sql VARCHAR2(4000);
v_head VARCHAR2(4000);
v_head_sql VARCHAR2(4000);
v_str_sql VARCHAR2(4000);
TYPE cv_typ IS REF CURSOR;
cv cv_typ;
begin
v_sql := p_sql;
OPEN cv FOR v_sql;
LOOP
FETCH cv INTO v_cu_string;
EXIT WHEN cv%NOTFOUND;
------ Processing steps
END LOOP;
CLOSE cv;
END;Thanksuser527060 wrote:
Following is my code and wanted to see if that can work with every query passed as a parameter.
Just curious as to why this is an improvement of
exec test1 ('select col1, col2, col3 from table1','Two columns Sql')
select col1, col2, col3 from table1And
exec test1 ('select col1 from table2','one columns Sql')
select col1 from table2And
exec test1 ('select col1, col2, col3, col4, col5 from table3','Five columns Sql')
select col1, col2, col3, col4, col5 from table3It needs more code from you to build, more code for anyone to enter to execute, limits selects to only one table I would guess also comes with less documentation than SQL.
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/toc.htm -
Two prices for one material in sales order according condition type
Hi,
I configure the condition type according to following condition record:
Plant/ Sales Organization/ Distribution channel/ Material / Customer
(because system should calculate two prices for one material in two plants)
In the sales order screen, when user firstly select material , quantity, plant , system calculate the related price correctly but if user select the material and quantity and enter and after that select the plant, system will show the error message in pricing procedure and to have a price for material, user should update the price in the price condition in sales order.
It is so difficult for user to update one by one, because there are several sales orders which need to update.
Is it any way to solve this problem?
Thanks,> In the sales order screen, when user firstly select material , quantity, plant , system calculate the related price correctly but if user select the material and quantity and enter and after that select the plant, system will show the error message in pricing
Hi,
Why to let the user press Enter button before entering plant value Obviously, system will give error, because it doesnt get all the required value based on which you have maintained condition records.
Ask the user to enter all the value at item level otherwise you will have go for the option that you are doing currently.
It is said prevention is better than cure
Hope you get some inputs
regards,
Sagar -
Calling the same API twice in one procedure
Has anyone every had any issue in caling an API twice in the one procedure?
I have one file that has two element entries and up to three salary components. Has anyone ever had an issue doing this?
Edited by: 859919 on May 21, 2011 9:26 AMAs my understanding ,There is no any restriction on calling API on number to time either from same procedure or other procedure.
What issue you are getting:)
Thanks -
Cursor in stored procedure fails
Hi,
In our application we have a stored procedure which is called with in parameters to generate data into some table. This procedure is being called from another procedure.
This procedure has been used to generate data into the table without any problem since oracle 9i. Last year we have upgraded to oracle 11g after that this procedure is failing intermittently to generate data. When this procedure is executed 50 times it fails atleast once to generate data. If we rerun the procedure for the failed case it generates the data without any change to program code nor any change in underlying data. It doesn't fail if we rerun. Therefore we are unable to simulate the problem.
Procedure has got a very simple code.
proc1 (p1 date,p2 date) is
begin
for c1_rec in c1 (select col1,col2
from x,y where x.......)
loop
end loop;
end;
First we thought for some reason this procedure was not executed at all. But it is not the case. Actually it calls the procedure but the cursor inside the procedure doesn't fetch any records. Also it doesn't report any oracle error.
Appreciate if any one can help me in this.
Thanks & Regards,
Rajavaidyanathanraja wrote:
4. If there is a logical error, the same procedure should not generate data when it is rerun. Behaviour should be the same at all times.Incorrect. Something like NLS settings for example can make the same code, behave differently. E.g. a date string is passed and implicitly converted to a date. And this will work for most dates from different session using different NLS settings (e.g. yyyy/mm/dd versus yyyy/dd/mm). And these sessions will provide different results using the same parameters calling the same application code.
There are a number of such run-time factors that influences code.
5. Failed means it didn't generate the expected output. Which means that there is a problem with that SQL being executed the way it is, with the parameters used. You need to isolate the problem further.
6. Parameter values are right.Have you proved that by using a test case that runs the very same SQL via a test proc, using the same parameter, via a job? Ran that test case interactively via sqlplus?
You need to pop the hood and isolate the problem.
7. I came across one blog saying different behaviour for REF CURSOR between oracle 10g and 11g and he says it is oracle bug. I don't know whether it is applicable for this case also.Bahumbug. There are many Oracle bugs. As there is in all software. However, you have not provided any evidence of a bug.
Application code is behaving inconsistently. That is the symptom. Oracle system code is not relevant until you can prove that the inconsistency is not in the application code, but lower down the call stack. -
Hi,
Hope someone can give me few ideas about this:
I have a database table from which I will take each record in a cursor at one time, use that record as IN parameters in a procedure to do modifications on another table and then once it completes it will take the next record from the cursor and execute the procedure again with the new IN parameters from the second record of table1.
Sample Code:
PROCEDURE p_procedure (p_id IN table1.id%TYPE,
p_date IN table1.date%TYPE,
p_code IN table1.code%TYPE) IS
CURSOR c1 IS
SELECT id, date, code
FROM table1
WHERE id = p_id
and date = p_date
and code = p_code;
BEGIN
For crec in c1 LOOP
BEGIN LOOP
UPDATE table2
SET ID = NULL, DATE = NULL, CODE = NULL, PRINT = NULL, TRAN = NULL --updates to columns in table2
WHERE table2.id = p_id
and table2.date = p_date
and table2.code = p_code;
COMMIT;
END LOOP;
END p_procedure;
Is this the right process or is my code wrong???
Thanks
ManojWhat is wrong in having a COMMIT inside a loop? It might only increase the I/O, but the code does work right?The COMMIT says you've finished and can release resources, the loop says you haven't finished and still need them. It's asking for trouble (traditionally in the form of ORA-01555 "Snapshot too old" errors, which confuse developers who thought they were doing the database a favour by saving their work like it was MS Word). It can also complicate restartability, although perversely developers sometimes claim it is necessary in order to allow restarting if a long-running process fails (which it isn't) or to allow monitoring of how far it's got.
At the very least, try COMMIT WRITE BATCH NOWAIT ;) -
Can we define cursors from in procedure while using .procedure file?
Hi Team,
Can we define cursors from in procedure while using .procedure file?
I have a catalog procedure in which am using cursors and it is working fine. But while trying to create the same procedure using .procedure , the validation is failing with "An error occurred while parsing your procedure".
Request your suggestions.
Regards,
Krishna TanguduHi Krishna,
I also tried to find the validation for procedure and failed. What was on my mind when I posted earlier was related to Window -> Properties -> SAP HANA -> Modeler -> Validation Rules but this is not the same for procedure.
Also the validation error seems to not being raised by Studio but the server, so it's not a frontend validation anyway.
Trying to replicate your error I create two procedures here and both worked fine with cursor.
The behavior you found was the same I found, the .procedure goes to repository and _SYS_BIC after activation and .hdbprocedure goes to catalog only.
As far as I understood as you are moving from catalog (manually) and it worked on catalog (.hdbprocedure), I could check the declaration of table types of header as it's the main difference.
Despite this investigation of declaration, you can try create it on modeler direct and see what you found on .procedure created. This can speed up investigation.
Regards, Fernando Da Rós
BTW: I'm faced an very strange behavior that each caracter I type on .procedure or .hdbprocedure freezes studio by around 15 seconds ?!?!?!?! (studio rev73)
Maybe you are looking for
-
Logon problem with oracle management server
I have problem with Oracle Management Server when I connect to the OMS message comes The exception (java.lang.NullPointerException) occurred. if any body have any idea pls tell us
-
HT1695 i am having trouble connecting to wifi on my i phone i have cellular service with at&t
I am having trouble connecting to wifi on my I-PHONE,I have at&t as my service
-
Created with second class object failure.
Hi All, When I am trying to import tables from the database, I got the following warning message. Only for some of the tables I am getting. The table is importing properly. But I don't know why I am getting this error. Please anyone can explain. Than
-
How do I make display full screen? It's only about half now.
Please help. Somehow I did something to get a display of have a screen. I want full. Tried "view" and help. I don't see that square with arrows help says to push.
-
Multiple inspection against Batch
Hi, My client doing the inprocess quality checking of the same set of inprocess characteristics every hour for a particular bacth during production. They have both REM and PP-PI and we are using 03 and 13 inspection types for inprocess checking.Now h