Home » SQL & PL/SQL » SQL & PL/SQL » Please help in this problem (TOAD)
icon4.gif  Please help in this problem [message #625840] Wed, 15 October 2014 00:36 Go to next message
praveen9728
Messages: 1
Registered: October 2014
Location: Delhi, India
Junior Member
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name, department_name
FROM hr.employees, hr.departments
WHERE hr.employees.department_id = hr.departments.department_id
AND hR.employees.department_id = 80;
--FOR UPDATE OF salary NOWAIT;
BEGIN
FOR table_name IN emp_cursor LOOP
DBMS_OUTPUT.PUT_LINE( table_name.employee_id||' '||table_name.last_name||' '||table_name.department_name );
COMMIT;
END LOOP;
END;
---
CREATE TABLE table_name ( employee_id NUMBER(5), last_name VARCHAR(10), department_name VARCHAR(10) );
---
SELECT * FROM table_name;

PROBLEM IS , NO DATA STORES IN table_name,why????
Re: Please help in this problem [message #625843 is a reply to message #625840] Wed, 15 October 2014 00:43 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
welcome to the forum!!

Use http://www.dpriver.com/pp/sqlformat.htm for code format
DECLARE 
    CURSOR emp_cursor IS 
      SELECT employee_id, 
             last_name, 
             department_name 
      FROM   hr.employees, 
             hr.departments 
      WHERE  hr.employees.department_id = hr.departments.department_id 
             AND hr.employees.department_id = 80; 
--FOR UPDATE OF salary NOWAIT; 
BEGIN 
    FOR table_name IN emp_cursor LOOP 
        dbms_output.Put_line(table_name.employee_id 
                             ||' ' 
                             ||table_name.last_name 
                             ||' ' 
                             ||table_name.department_name); 

        COMMIT; 
    END LOOP; 
END; 

what is your requirement?
Re: Please help in this problem [message #625846 is a reply to message #625840] Wed, 15 October 2014 00:53 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi,

CREATE TABLE table_name ( empno NUMBER(5), ename VARCHAR(10), dname VARCHAR(10) );

DECLARE 
    CURSOR emp_cursor 
    IS 
      SELECT empno, 
             ename, 
             a.deptno, 
             dname 
      FROM   emp a, 
             dept b 
      WHERE  a.deptno = b.deptno 
             AND a.deptno = 10; 
--FOR UPDATE OF salary NOWAIT;  
BEGIN 
    FOR emp_rec IN emp_cursor LOOP 
        INSERT INTO table_name 
        VALUES     (emp_rec.empno, 
                    emp_rec.ename, 
                    emp_rec.dname); 

        COMMIT; 
    END LOOP; 
END; 
Re: Please help in this problem [message #625847 is a reply to message #625840] Wed, 15 October 2014 00:54 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
praveen9728 wrote on Wed, 15 October 2014 11:10

NO DATA FOUND IN table_name why????


1. Welcome to the forum!

Please read and follow the OraFAQ Forum Guide and How to use [code] tags, to enable us to help you.

2. Why have posted the same question thrice?

3. It works for me,

SQL> set serveroutput on;
SQL> DROP TABLE table_name purge
  2  /

Table dropped.

SQL>
SQL> CREATE TABLE table_name ( empno NUMBER(5), ename VARCHAR(10), deptno number(2))
  2  /

Table created.

SQL>
SQL> DECLARE
  2      CURSOR emp_cursor IS
  3        SELECT e.empno,
  4               e.ename,
  5               e.deptno
  6        FROM   emp e,
  7               dept d
  8        WHERE  e.deptno = d.deptno
  9        AND    e.deptno = 10;
 10  --FOR UPDATE OF salary NOWAIT;
 11  BEGIN
 12      FOR table_name IN emp_cursor LOOP
 13          INSERT INTO table_name
 14          VALUES     (table_name.empno,
 15                      table_name.ename,
 16                      table_name.deptno);
 17      END LOOP;
 18  END;
 19  /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from table_name
  2  /

     EMPNO ENAME          DEPTNO
---------- ---------- ----------
      7782 CLARK              10
      7839 KING               10
      7934 MILLER             10

SQL>


Because, the filter department = 10 returns result in my case. Did you check whether any rows exist for department = 80? First execute the select query and verify.

4. what is the purpose of the PL/SQL code? Isn't it just a simple SQL?

5. Topic name "Please help in this problem" is so meaningless. Please remember to provide an explicit topic name.


Regards,
Lalit

Edit : Removed dbms_output and added the insert statement.

[Updated on: Wed, 15 October 2014 01:02]

Report message to a moderator

Re: Please help in this problem [message #625865 is a reply to message #625847] Wed, 15 October 2014 02:49 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Just to be absolutely clear the table_name in
FOR table_name IN emp_cursor LOOP

Is a record group created by the for loop and it only exists inside the for loop.
It has no relationship to an actual oracle table of the same name.
To insert data from the cursor to the table you need to write an insert statement as mist and lalit both did.

Or, as lalit suggested, you could just remove the cursor and write a single insert/select statement to do the same job.
Re: Please help in this problem [message #625869 is a reply to message #625865] Wed, 15 October 2014 03:07 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Create table as select.. at one go.
Re: Please help in this problem [message #625870 is a reply to message #625869] Wed, 15 October 2014 03:10 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And once the table is created?
Create table as select is only used in rare cases, not general code, and given the OPs level of knowledge that fact should be pointed out.
Re: Please help in this problem [message #625871 is a reply to message #625870] Wed, 15 October 2014 03:18 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I am just talking about this one time, as OP would create the table, then insert..select. I am not sure what OP is trying to achieve, perhaps learning PL/SQL.
Previous Topic: DBMS_SCHEDULER.Create_Job related Error
Next Topic: complex view
Goto Forum:
  


Current Time: Sat Apr 20 00:25:14 CDT 2024