small question I get the error message...

Hi refugees,

I am trying to run... but I get this error... where am I wrong in this
to display records in the INDEX BY ename...

{
DECLARE
TYPE emp_typ IS TABLE OF emp.sal%Type INDEX OF emp.ename%TYPE;
emp_arr emp_typ;

CURSOR c_emp IS SELECT ename, sal FROM emp;
r_emp c_emp % ROWTYPE;
v_idx VARCHAR2 (20);
num_total_rows NUMBER;
BEGIN
FOR r_emp IN c_emp
LOOP
emp_arr (r_emp.ename): = r_emp.sal;
num_total_rows: = c_emp % ROWCOUNT;
END LOOP;

DBMS_OUTPUT. Put_line (num_total_rows);

I'm IN emp_arr... FIRST emp_arr LAST.
LOOP
v_idx: = emp_arr first.
DBMS_OUTPUT. Put_line (v_idx |) ',' || To_char (emp_arr (v_idx)));
v_idx: = emp_arr. NEXT (v_idx);

END LOOP;

END;
/

DECLARE
*
ERROR on line 1:
ORA-06502: PL/SQL: digital or value error: character of number conversion error
ORA-06512: at line 18 level

}


Thank you

Published by: SeenuGuddu on September 20, 2009 02:22

Hello

SeenuGuddu wrote:
Thank you Frank Kulash, michaels2
I'm fighting for a long time...
the question is... the code is below... is that ok or any changes I have to do

That is really good!

I have two very minor ideas:

(1) you have probably issued a command like

SET  SERVEROUTPUT  ON  SIZE 50000

otherwise, you don't see any output of dbms_output.

You must include this command in the same script. It makes no sense to run the DECLARE block without it, and it when it is not strictly necessary does not take much time. (This is not a case where the effectiveness is a high priority.)

(2)

Define a PL/SQL table that has only a single column that stores the number with the index as type varchar2.
Store the emp.sal in the PL/SQL table with ename like index.
Define a cursor of emp table.
To loop through the cursor and display the name of the cursor variable and the salary of the collection for each employee.

This looks like a duty. As I read, a LOOP (the cursor loop FOR) was all that was required. You were in the class, I was not: so I could be wrong. In any case, it doesn't hurt to show that you know how to loop through the collection, but also how to loop through the cursor, but you should know that it is possible to read and write in the same loop:

DECLARE
    TYPE emp_typ IS TABLE OF emp.sal%Type INDEX BY emp.ename%TYPE;
    emp_arr         emp_typ;

    CURSOR c_emp IS SELECT ename,sal FROM emp;
    r_emp  c_emp%ROWTYPE;
    v_idx VARCHAR2(20);
    num_total_rows NUMBER;
BEGIN
    FOR r_emp IN c_emp
    LOOP
        emp_arr(r_emp.ename) := r_emp.sal;
        num_total_rows := c_emp%ROWCOUNT ;
--      DBMS_OUTPUT.PUT_LINE( r_emp.ename || ',' || TO_CHAR (emp_arr(r_emp.ename)) );  -- Could display here, and skip the other loop
    END LOOP;

    DBMS_OUTPUT.PUT_LINE(  num_total_rows );

     v_idx := emp_arr.FIRST;

    WHILE v_idx IS NOT NULL
    LOOP
        DBMS_OUTPUT.PUT_LINE( v_idx || ',' || TO_CHAR(emp_arr(v_idx)) );
        v_idx := emp_arr.NEXT( v_idx );

    END LOOP;

END;
/

Nice formatting!

Tags: Database

Similar Questions

Maybe you are looking for