Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help understanding cursors, scope, variables, and looping.

594752Oct 3 2007 — edited Oct 5 2007

I know this is a little complicated, but I have a problem with it and maybe someone can help me figure out my logic here. This query works fairly well, but oddly misses some things here and there. What I am trying to do is create a cursor on on column of one table. This provide a list of values to get a corresponding value for. So the list would be columnA and the value to insert would go in columnB and would be gotten through the process in the inner loop. So I have an outer with the distinct values for the list and the place for the value brought back by the inner loop.

Now the inner loop returns 0, 1, or many records. The value returned to the outer loop columnB will be 'NO' if no records are found in the inner loop (fetch). The value returned will be 'Nuvox Switch Only' if ALL the records returned contained something that would evaluate to Nuvox Switch Only - see the decode statement. And finally, if there were recordss returned, but not all qualified for 'Nuvox Switch only' then the value returned would be 'YES'

What I am trying to do is initialize the v_ALocValidUpdateValue to 'NO' before the first outer loop and after the UPDATE statement at the bottom of the outer loop and after the inner loop closes for that iteration. I am using v_ALocValid for the value for each inner record. This is initialized to 'No' before the beginning of the inner loop and at the bottom of the loop. It's value as 'YES' is used to update v_ALocValidUpdateValue to 'YES' and if that happens, it is supposed to drop out of the loop. The reason for this is that since there are records, it cannot return 'NO' and since at least one of the records is a 'YES', it can't be 'Nuvox Switch Only'. Each time through the loop, as long as there are no 'YES's, then it keeps assigning 'Nuvox Switch Only' to v_ALocValidUpdateValue and finishes when it is out of corresponding records. All this works most of the time. But I occasionally get one where a value of 'NO' is assigned when there is a record. In that case it should be either 'Nuvox Switch only' or in most cases should be 'YES'.

PROCEDURE ValidPerLerg_ALOC_PROC IS
	v_ALoc      VARCHAR2(17);
	v_ALocValid VARCHAR2(20);
	v_ColumnALOC RCO.RPT_DS1_CNT_CAT.ALOC%TYPE;
	v_OCN       VARCHAR2(20);
	v_OCN_NAME  VARCHAR2(50);
	v_ALocValidUpdateValue VARCHAR2(17);
	CURSOR ALOC_CURSOR IS
	SELECT DISTINCT (ALOC)
	FROM RCO.RPT_DS1_CNT_CAT
	WHERE TRIM(ALOC_VALID) IS NOT NULL;
	CURSOR ALOC_VALID_CURSOR IS
	SELECT   SUBSTR(six.SWITCH,1,8) AS COLUMNALOC
                        ,six.OCN
                        ,one.OCN_NAME
			,decode(1,sign(instr(upper(one.OCN_NAME),'NUVOX')),'Nuvox Switch only'
				 ,sign(instr(upper(one.OCN_NAME),'NEW SOUTH')),'Nuvox Switch only'
			         ,sign(instr(upper(one.OCN_NAME),'TRIVERGENT')),'Nuvox Switch only'
		  	         ,sign(instr(upper(one.OCN_NAME),'GABRIEL')),'Nuvox Switch only','YES') VALID
   	FROM      TRAFFIC.LERG_6 @ traffp six
	                        ,TRAFFIC.LERG_1 @ traffp one
	WHERE     one.ocn_# = six.ocn
	AND       SUBSTR(six.SWITCH,1,8) IN ( v_ALoc )
	GROUP BY  SUBSTR(six.SWITCH,1,8)
	                        ,six.OCN
	                        ,one.OCN_NAME;
	BEGIN
		OPEN ALOC_CURSOR;
		-- Initialize before first of either values
		-- value that is finally updated - 'N/A for no records; 'YES' for any records; 'Nuvox Switch only' if all records are 'Nuvox'
		v_ALocValidUpdateValue :='NO';
		LOOP
			FETCH ALOC_CURSOR INTO v_ALoc;
		    EXIT WHEN ALOC_CURSOR%notfound;
		    BEGIN
				OPEN ALOC_VALID_CURSOR;
				-- Set to NO in case no records are found - will then be updated to NO
				v_ALocValid := 'NO';             -- Each individual record in subquery
				LOOP
					FETCH ALOC_VALID_CURSOR INTO v_ColumnALOC, v_OCN, v_OCN_Name, v_ALocValid;
					EXIT WHEN ALOC_VALID_CURSOR%notfound;
					-- Now that there are records, v_ALocValidUpdateValue='YES'
					v_ALocValidUpdateValue := 'YES';
					IF v_ALocValid = 'YES' THEN
						v_ALocValidUpdateValue := 'YES';
						EXIT;
					ELSE v_ALocValidUpdateValue := 'Nuvox Switch only';
					END IF;
				    v_ALocValid := 'NO';
				END LOOP;
				CLOSE ALOC_VALID_CURSOR;
				-- Update the column value
				UPDATE RCO.RPT_DS1_CNT_CAT
				SET ALOC_VALID = v_ALocValidUpdateValue
				WHERE ALOC = v_ColumnALOC;
				v_ALocValidUpdateValue :='NO';   -- Receives value of 'Nuvox Swicth Only' if ALL results are 'Nuvox..', 'YES' if not all 'Nuvox ...'
				v_ALocValid := 'NO';
			END;
		END LOOP;
		CLOSE ALOC_CURSOR;
	COMMIT;
	END; -- ValidPerLerg_ALOC_PROC

I wrote this query and I am quite new at Oracle and have it almost right, but I am starting to be stepped on for a deadline and am afraid since I am new here that I could lose my job if I don't figure it out. This is part of a much larger package, most of which is working flawlessly. I was tasked with automating a copy/paste Excel query back and forth operation that took hours to do. I now have it down to ACCEPTING 2 date variables and hitting ENTER and it runs by itself behind the scenes and produces the reports. I could really use some help on this. I think the problem is my logic in the inner loop. I still don't fully understand PL/SQL variables, scope, cursors, and loop structures. I have basically jumped in with both feet and get myself over my head sometimes. Thank you all for your understanding.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2007
Added on Oct 3 2007
17 comments
1,356 views