Home » SQL & PL/SQL » SQL & PL/SQL » Problem In Accessing The value of Outer Cursor in inner Cursor ? (oracle, 11g r2, windows 7 64 bit)
Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602703] Sat, 07 December 2013 06:03 Go to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
i have a procedure that have tow cursor and they are nested.the outer cursor move on a relation_table and the inner cursor move on a pk_table ((this table have all the pk)).in this procedure the first cursor check every rows of relation_table and get the value of a specific field ((this value is one of the pk_table column)).the second cursor for every rows of first cursor loop on a pk_table. in the second cursor i want to get a field from a column that that its name is in the first cursor, some thing like this :
inner_cursor.outer_cursor.field
which the
outer_cursor.field
return a column name that is i want to use in the inner cursor.well i cant do this even in this shape:
inner_cursor.(outer_cursor.field);
. does any body know how to handle this?
my tables:
CREATE TABLE BIG_TABLE
(
   ID VARCHAR2(50),
   Pk_A VARCHAR2(50),      
   PK_B VARCHAR2(50),
   PK_C VARCHAR2(50),
   PK_D VARCHAR2(50)
);

this table column is the pk of my tables(A,B,C,D)
CREATE TABLE RELATION_TABLE
(
   ID VARCHAR2(50),
   SOURCE_TABLE VARCHAR2(50),
   SOURCE_PK VARCHAR2(50),
   SOURCE_FK VARCHAR2(50),
   DESTINATION_TABLE VARCHAR2(50),
   DESTINATION_PK VARCHAR2(50)
);

this table store the relations between tables.
and my procedure (ofcource this is part of my procedure):
CREATE OR REPLACE PROCEDURE TEST1                                           
AS
	CURSOR loop_relation IS
		SELECT * FROM RELATION_table;
	relation_rec loop_relation%rowtype;
	CURSOR loop_pk_TABLE IS
		SELECT * FROM pk_TABLE;
	pk_TABLE_rec loop_pk_TABLE%rowtype;
BEGIN
	FOR RELATION_REC IN LOOP_RELATION
	LOOP
		FOR pk_TABLE_rec in loop_pk_TABLE
		LOOP
			IF (pk_TABLE_REC.RELATION_REC.DESTINATION_PK IS NULL) THEN    --MY PROBLEM,I CANT USE THREE DOTS
				UPDATE PK_TABLE
				SET PK_TABLE.RELATION_REC.DESTINATION_PK = 'NOT IMPORTANT'  --OOPS, ANOTHER THREE DOTS
				WHERE PK_TABLE_REC.ID = PK_TABLE.ID;
			END IF;
		END LOOP;
	END LOOP;	
END TEST1;
/
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602704 is a reply to message #602703] Sat, 07 December 2013 06:12 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Your main issue is that you are using a pl/sql loop to do what could be done in SQL. Not a good idea. Just use SQL
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602705 is a reply to message #602704] Sat, 07 December 2013 06:28 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
sorry if my questions may be silly(im new in database)
i did not write the whole procedure.its too long.first i tried to do just with sql but with some problems i forced to use pl/sql.
any way is not any way to use 2 dots to access a value. can i make a alias for the one of dots and then use it. some thing like this:
inner_cursor.outer_cursor.field

make alias for
outer_cursor.field
as M and then make it like this:
inner_cursor.M

i do not know how to make that alias!!
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602710 is a reply to message #602703] Sat, 07 December 2013 09:08 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
I have the feeling that you are from the java world (JPA or so). Maybe I'm completely mistaken so don't be offended.
If that's the case, you should forget all OO stuff, and think relational, you're in the database world.

Can you give a small example of the input and the desired output?

I believe that the words of pablolee hold true, this probably can be done with one or two sql-statements.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602711 is a reply to message #602710] Sat, 07 December 2013 09:26 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>this table column is the pk of my tables(A,B,C,D)
which is a total of 200 (50*4) characters long
then how can FK be only "SOURCE_FK VARCHAR2(50)" ?

I could show you, but refuse to instill bad skills to novice coder.
NEVER do in PL/SQL that which can be done in plain SQL
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602712 is a reply to message #602705] Sat, 07 December 2013 09:54 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
And what inner_cursor.outer_cursor.field is supposed to mean? There is no correlation between two cursors. Your inner loop simply fetches same data from pk_TABLE over and over. And what is that pk_TABLE? There is only BIG_TABLE & RELATION_table in your post. You need to describe logic you need to implement in words.

SY.
P.S. FOR CURSOR loop variable scope is local to loop, therefore variable pk_TABLE_rec declared as pk_TABLE_rec loop_pk_TABLE%rowtype; and variable pk_TABLE_rec declared by FOR pk_TABLE_rec in loop_pk_TABLE are two different variables.

SY.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602713 is a reply to message #602712] Sat, 07 December 2013 10:52 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
thanque for your answers
its the first time i'm writing pl/sql so maybe i'm wrong
and big_table is actually pk_table,my fault
what i want to do is to build a table that stores the relation between all tables base on primary key of tables some thing like UNnormalization, and please don't ask why!

i mean i have a table that it's columns are the pk of tables.and if a row from table A and a row from table B are in relation, so the pk of them should be in the same row in pk_table.
i'm sorry if i can't to explain my problem understandable,but i'm sure it has a solution and i will find it. i let you know when i find it.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602714 is a reply to message #602713] Sat, 07 December 2013 11:36 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Still very unclear. For example, table SCOTT.DEPT has a PK on DEPTNO column, table EMP has PK on EMPNO column and FK on DEPTNO column referencing table SCOTT.DEPT. So how this will be depicted in RELATION_TABLE and PK_TABLE.

SY.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602727 is a reply to message #602714] Sun, 08 December 2013 00:44 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
it will be like this:
in relation table:(id, source_table, source_pk, source_fk, destination_table, destination_pk) = (id, EMP, EMPNO, DEPTNO, SCOTT.DEPT, DEPTNO)
in pk_table we have 'EMPNO' and 'DEPTNO' as column. if we have a row in 'SCOTT.DEPT' which iT'S PK 'DEPTNO' is '9', and we have a row in 'EMP' which it's pk is '50' and it's fk is '9', so we have a row in pk_table that EMPNO value is '50' and DEPTNO value is '9'.

i think it's not important to understand the system. what i want to know is how to use tow dots(.). like (cursor1.cursor2.cursor2_field1;), and (cursor2_field1) will return a field of cursor1. i don't know how to explain this to stupid oracle!!
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602745 is a reply to message #602727] Sun, 08 December 2013 06:00 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
You're right, oracle is stupid, it will only do exactly as it is told. If it's not doing what you want it to do, than that is your fault, not Oracle's. Based on your descriptions so far (which have been poorly structured and very difficult to understand), you are going about things the wrong way. Once again, it looks very much like you should be using pure sql and not pl/sql. You will find that on these forums, few people will help you implement bad design, which means that we have to be convinced that what you're doing is NOT bad design. You have failed to do this so far.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602747 is a reply to message #602745] Sun, 08 December 2013 06:33 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
I FOUND IT Surprised
I FINALLY FOUND A WAY TO DO IT
THE WAY TO DO IT IS TO PUT THE SECOND CURSOR AND IF STATEMENT INTO A PROCEDURE AND PUT THE PROCEDURE INTO A STRING VARIABLE.
THEN FOR IN SITUATION LIKE THIS IN STRING VARIBLE :(INNER_CURSOR.OUTER_CURSOR.FIELD) YOU SHOULD DO THIS:

INNER_CURSOR.'||OUTER_CURSOR.FIELD||'
AND THEN EXECUTE THE VARIABLE.
I'M SORRY IF I CAN'T EXPLAIN CLEARLY BUT I WILL PUT A SAMPLE CODE TO SEE.(BUT WITH A DELAY)
THANK YOU ALL ORACLE DUDES
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602753 is a reply to message #602727] Sun, 08 December 2013 08:00 Go to previous messageGo to next message
Its_me_ved
Messages: 979
Registered: October 2009
Location: India
Senior Member
Quote:

it will be like this:
in relation table:(id, source_table, source_pk, source_fk, destination_table, destination_pk) = (id, EMP, EMPNO, DEPTNO, SCOTT.DEPT, DEPTNO)
in pk_table we have 'EMPNO' and 'DEPTNO' as column. if we have a row in 'SCOTT.DEPT' which iT'S PK 'DEPTNO' is '9', and we have a row in 'EMP' which it's pk is '50' and it's fk is '9', so we have a row in pk_table that EMPNO value is '50' and DEPTNO value is '9'.

i think it's not important to understand the system


Before jumping into a problem, we should always try to understand/analyze the problem well.
For me, you have not understand your requirement well.
Or, if you have understand the requirement well, i should ask you what your objective is.The whole approach appears to be flawed to me.

Regards
Ved
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602766 is a reply to message #602747] Sun, 08 December 2013 10:50 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
You have chosen to ignore the advice of people significantly more experienced than you in this field. I have zero sympathy for the problems that you will face by implementing this approach.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602855 is a reply to message #602766] Mon, 09 December 2013 14:35 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
a simple quick example
declare
junk number;
begin
for pnt in (select col1 from my_table order by col1) loop
  for pnt2 in (select col2 from their_table order by col1) loop
     junk := pnt.col1 + pnt2.col2;
  end loop;
end loop;
end;
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602857 is a reply to message #602855] Mon, 09 December 2013 14:52 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
row by row is slow by slow
NEVER do in PL/SQL that which can be done in plain SQL

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/

Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602873 is a reply to message #602766] Tue, 10 December 2013 01:15 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
i think you didn't get my problem correctly (actually my fault,can't express my problem explicit,probably because i'm novice)
i actually explained my problem to some experienced oracle man in person and they confirmed my problem.
i really cant do it with pure sql, i really tried after your advise but it's impossible
please do not continue this topic,i have no more time to think for alternative solution,i decided to continue on my solution
anyway, thank you for your help
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602891 is a reply to message #602873] Tue, 10 December 2013 02:45 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
jalal-rasooly wrote on Tue, 10 December 2013 07:15
i think you didn't get my problem correctly
on that we agree.
Quote:
(actually my fault,can't express my problem explicit,probably because i'm novice)
yes it is your fault, not because you are a novice, but because you have failed to provide a valid test case and explanation of what you're trying to do.
Quote:
i actually explained my problem to some experienced oracle man in person and they confirmed my problem.
Based on what you've posted here, I'm going to disagree with your 'experienced oracle man'
Quote:
i really cant do it with pure sql, i really tried after your advise but it's impossible
Bull. If you tried, then post your attempts. You haven't bothered to try to explain the actual problem properly, you have fixed a 'solution' in your head and decided that that is the way that you need to go and anyone who disagrees is wrong.
Quote:
please do not continue this topic,
That is your choice to continue or not.
Quote:
i have no more time to think for alternative solution,i decided to continue on my solution
anyway, thank you for your help

That is your choice, it's crappy choices like that that keep people like me in contracts.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602904 is a reply to message #602891] Tue, 10 December 2013 05:22 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
i don't understand why are you angry((maybe because i found a solution and you didn't))!! we have no enmity Smile
By The Way,base on a time you spend on this forum to insult to others, i think i have more contracts than you Laughing
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602922 is a reply to message #602904] Tue, 10 December 2013 06:52 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
jalal-rasooly wrote on Tue, 10 December 2013 11:22
i don't understand why are you angry((maybe because i found a solution and you didn't))!! we have no enmity Smile
I'm not angry, I simply don't care enough to be angry. However, since enmity is simply a state of active opposition, yes, there is enmity between us.
Quote:
By The Way,base on a time you spend on this forum to insult to others,
I have not insulted you, I have pointed out that crappy decisions keep me busy, Quote:
i think i have more contracts than you Laughing
I can guarantee that this is not the case.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #602943 is a reply to message #602922] Tue, 10 December 2013 08:52 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>,i decided to continue on my solution anyway, thank you for your help
please post your solution here
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #603155 is a reply to message #602943] Wed, 11 December 2013 15:24 Go to previous messageGo to next message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
in example procedure i wrote before, the part like:
PK_TABLE.RELATION_REC.DESTINATION_PK
that had tow dot was problem.
i changed procedure to this an it works great.
CREATE OR REPLACE PROCEDURE TEST1                                           
AS
	CURSOR loop_relation IS
		SELECT * FROM RELATION_table;
	relation_rec loop_relation%rowtype;
        VAR1 VARCHAR2(4000);
        VAR2 VARCHAR2(4000);
BEGIN
	FOR RELATION_REC IN LOOP_RELATION
	LOOP
                VAR := ' CREATE OR REPLACE PROCEDURE TEST AS
                            CURSOR loop_pk_TABLE IS
                               SELECT * FROM pk_TABLE;
                         BEGIN
		            FOR pk_TABLE_rec in loop_pk_TABLE
		            LOOP
			       IF (pk_TABLE_REC.'||RELATION_REC.DESTINATION_PK||' IS NULL) THEN    --MY PROBLEM WAS I COULDN'T USE TOW DOTS
				  UPDATE PK_TABLE
				  SET PK_TABLE.'||RELATION_REC.DESTINATION_PK'|| = (IT CAN BE ANY THING,IN MY MAIN CODE IS A QUERY SELECT)  --ANOTHER TOW DOTS
				  WHERE PK_TABLE_REC.ID = PK_TABLE.ID;
			       END IF;
		            END LOOP;
                         END TEST;'
                    EXECUTE IMMEDIATE VAR;
                    var2 := 'begin test(); end ;';
                    EXECUTE IMMEDIATE VAR2;
	END LOOP;	
END TEST1;
/

I THINK IT'S OBVIOUS WHAT I DID.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #603157 is a reply to message #603155] Wed, 11 December 2013 18:15 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>i changed procedure to this an it works great.
IMO, "great" is not appropriate or correct.
dynamic SQL (EXECUTE IMMEDIATE) does not scale.
poor performance will result if procedure is (heavily?) used in a multiuser OLTP application.
Re: Problem In Accessing The value of Outer Cursor in inner Cursor ? [message #603183 is a reply to message #603157] Thu, 12 December 2013 01:54 Go to previous message
jalal-rasooly
Messages: 11
Registered: November 2013
Junior Member
actually this procedure will not run consecutively. it will make the PK_TABLE once and after that for every change in tables it will update the PK_TABLE.
so pure performance does not make the big problem.
THE MOST IMPORTANT REASON i choosed this way is because honestly it's the only way i found AND also i have close deadline.
i will consider an optimized solution for later changes.
thank you
Previous Topic: To get Last week ?
Next Topic: Fetch data with no rows in a table
Goto Forum:
  


Current Time: Thu Apr 25 11:05:50 CDT 2024