Solved

Referncing Columns In a Cursor By Index

Posted on 2009-07-15
5
544 Views
Last Modified: 2013-12-07
Is there a way to reference columns in a cursor by index rather than by column name?  For instance,

DECLARE
  CURSOR myCursor IS
  SELECT first_name,
    last_name
  FROM customer;
BEGIN
  FOR curRecord IN myCursor LOOP
    DBMS_OUTPUT.PUT_LINE(curRecord(1));
    DBMS_OUTPUT.PUT_LINE(curRecord(2));
  END LOOP;
END;
0
Comment
Question by:ccovell
  • 2
5 Comments
 
LVL 48

Expert Comment

by:schwertner
ID: 24861676

So far only this can work

DECLARE
  CURSOR myCursor IS
  SELECT first_name,
    last_name
  FROM employees;
BEGIN
  FOR curRecord IN myCursor LOOP
    DBMS_OUTPUT.PUT_LINE(curRecord.first_name));
    DBMS_OUTPUT.PUT_LINE(curRecord.last_name));
  END LOOP;
END;
0
 

Author Comment

by:ccovell
ID: 24861730
ty schwertner,
I've come to the conclusion that this functionality is definitely no available.  Kind sad if your in a situation like me where you have a multi union query and there is no consistent name for the same column in each of the union components.  I've resided to giving them all the same alias and just using the alias reference in the cursor.
0
 
LVL 20

Accepted Solution

by:
flow01 earned 250 total points
ID: 24861846
Using same alias everywhere in the union is neat programming .
Until know however (in my experience) you can reference the columns of the union by the columns_names of the first union part.
0
 
LVL 48

Assisted Solution

by:schwertner
schwertner earned 250 total points
ID: 24862188
In UNION query the column names of the first query are used in the set statement. Also aliases could be given.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question