Hi, How do we display the column values of a table using cursors without knowing the column names inside the loop?

Showing Answers 1 - 15 of 15 Answers

Gopal

  • Oct 22nd, 2006
 

Declare a variable using PL/SQL %ROWTPE ( l_Record EMP%ROWTYPE)In the cursor, you can say .. cursor cursorname is select * from emp..Open cursor; loop .. fetch cursorname into l_Record.. Display the values from l_Record using dbms_output.put_line.Close the loop and cursor

  Was this answer useful?  Yes

Sanjib

  • Nov 2nd, 2006
 

Please, provide an example.

  Was this answer useful?  Yes

Sachin Vaidya

  • Nov 6th, 2006
 

I Don't think so, If you are trying to populate  the data using Records, then also you should  know name of the columns from the table or select query of the cursor. 

  Was this answer useful?  Yes

Raghavendra Jha

  • Nov 11th, 2006
 

No.its not possible to fetch column value from a cursor without knowing the column name,there is nothing to do with cursor,cursor is nothing but explicitly created memory area to fetch record, using sql query itself..and its not possible to retrieve any value without knowing column name,if it is not like that, plz expalin with an example..

  Was this answer useful?  Yes

Naresh Dantu

  • Nov 13th, 2006
 

You Can. Below is the example.

DECLARE

CURSOR cr_data

IS

SELECT ROWID, a.*

FROM fnd_user a where rownum < 10;

l_table_name VARCHAR2(2000) := 'FND_USER';

-- IMP--This table name should be same as your from table in the above cursor

l_value VARCHAR2 (2000);

l_str VARCHAR2 (2000);

CURSOR column_names (p_table_name VARCHAR2)

IS

SELECT *

FROM all_tab_columns

WHERE table_name = p_table_name;

-- You can use order by clause here if you want.

BEGIN

FOR cr_rec IN cr_data

LOOP

-- We should pass the same Table Name

FOR cr_columc_rec IN column_names (l_table_name)

LOOP

l_str :=

'Select '

|| cr_columc_rec.column_name

|| ' from '||l_table_name||' where rowid = '||chr(39)

|| cr_rec.ROWID||chr(39);

DBMS_OUTPUT.put_line ('Query is ' || l_str);

EXECUTE IMMEDIATE l_str

INTO l_value;

DBMS_OUTPUT.put_line ( 'Column is '

|| cr_columc_rec.column_name

|| ' and Value is '

|| l_value

);

END LOOP;

END LOOP;

END;

  Was this answer useful?  Yes

sankar

  • Nov 14th, 2007
 

We have prdefined table 'col'. from this table we can find the column names dynamically.
For ex.
select cname from col where col=1 and tname='emp'
then you will get first clomun name of emp table; ex. for in 1..2 loop select cname into cname1 from col where col=i and tname='emp';
cursor ec is select cname1 from emp;
end loop;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions