Home » SQL & PL/SQL » SQL & PL/SQL » EXECUTE IMMEDIATE trowing an error in PL/SQL Block (oracle 11g R2 WIN 7)
EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627144] Fri, 07 November 2014 20:56 Go to next message
9390512774
Messages: 103
Registered: January 2011
Location: hyd
Senior Member
Hi Expects,

I want to use "EXECUTE IMMEDIATE" Stmt to create a table in my pl/sql block. This block is giving an error when I try to execute the code without the existence of the table "TEMP_EMPLOYESS". If the table exists then it's working.

HELP ME how can achieve this issue.

DECLARE
   Stmt 	VARCHAR2(500);
   TYPE Temp_Employees IS TABLE OF HR.EMPLOYEES%ROWTYPE;
  v_Temp_Employees Temp_Employees;
  v_lp_cnt  NUMBER:=0;
  v_temp_table NUMBER:=-1;
   CURSOR CUR_EMPLOYEES 
      IS 
   SELECT * FROM EMPLOYEES;
BEGIN

  BEGIN
    SELECT 1 INTO v_temp_table FROM ALL_TABLES WHERE TABLE_NAME='TEMP_EMPLOYESS';
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
    v_temp_table:=0;
  END;

  IF v_temp_table=1 THEN
    --If table exists drop it and recreate
    EXECUTE IMMEDIATE 'DROP TABLE Temp_Employess';
    Stmt :='CREATE GLOBAL TEMPORARY TABLE Temp_Employess ON COMMIT DELETE ROWS AS SELECT * FROM EMPLOYEES WHERE 1=2';
    EXECUTE IMMEDIATE Stmt;
  ELSE
     --if table not exists create
    Stmt :='CREATE GLOBAL TEMPORARY TABLE Temp_Employess ON COMMIT DELETE ROWS AS SELECT * FROM EMPLOYEES WHERE 1=2';
    EXECUTE IMMEDIATE Stmt;
  END IF;

  OPEN CUR_EMPLOYEES;

   LOOP
       FETCH CUR_EMPLOYEES BULK COLLECT INTO v_Temp_Employees LIMIT 10;
       EXIT WHEN v_Temp_Employees.COUNT=0;
       v_lp_cnt:=v_lp_cnt+1;
            BEGIN
                FORALL idx IN v_Temp_Employees.FIRST..v_Temp_Employees.LAST SAVE EXCEPTIONS
                     INSERT INTO Temp_Employess VALUES(v_Temp_Employees(idx).EMPLOYEE_ID,    v_Temp_Employees(idx).FIRST_NAME,
                                                        v_Temp_Employees(idx).LAST_NAME,      v_Temp_Employees(idx).EMAIL,
                                                        v_Temp_Employees(idx).PHONE_NUMBER,   v_Temp_Employees(idx).HIRE_DATE,
                                                        v_Temp_Employees(idx).JOB_ID,         v_Temp_Employees(idx).SALARY,
                                                        v_Temp_Employees(idx).COMMISSION_PCT, v_Temp_Employees(idx).MANAGER_ID,
                                                        v_Temp_Employees(idx).DEPARTMENT_ID
                                                       );
                     COMMIT;
           EXCEPTION
                WHEN OTHERS THEN 
                     DBMS_OUTPUT.PUT_LINE('Exception in FORALL');
           END;
   END LOOP;
   
   CLOSE CUR_EMPLOYEES;
    
  FOR idx IN (SELECT * FROM Temp_Employess)
        LOOP
          DBMS_OUTPUT.PUT_LINE('EMPLOYEE_ID:'||idx.employee_id);
        END LOOP;

EXCEPTION
  WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE(SQLCODE);
  DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;

Re: EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627145 is a reply to message #627144] Fri, 07 November 2014 21:05 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
GLOBAL TEMPORARY TABLE should be created once using static SQL & then used as necessary for any & all subsequent sessions.

What do you think is the advantage to DROP & CREATE it every time this procedure runs?
Re: EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627146 is a reply to message #627145] Fri, 07 November 2014 21:12 Go to previous messageGo to next message
9390512774
Messages: 103
Registered: January 2011
Location: hyd
Senior Member
It is Applicable for
CREATE TABLE Temp_Employess  AS SELECT * FROM EMPLOYEES WHERE 1=2
also right?
Re: EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627149 is a reply to message #627146] Fri, 07 November 2014 21:51 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
9390512774 wrote on Fri, 07 November 2014 19:12
It is Applicable for
CREATE TABLE Temp_Employess  AS SELECT * FROM EMPLOYEES WHERE 1=2
also right?


Not correct for above, but above should be done as below

TRUNCATE TABLE TEMP_EMPLOYEES; -- which retains any & all INDEX on table.

DROP TABLE also removes all INDEX on the table.
Re: EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627159 is a reply to message #627144] Sat, 08 November 2014 01:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
           EXCEPTION
                WHEN OTHERS THEN 
                     DBMS_OUTPUT.PUT_LINE('Exception in FORALL');

Quote:
EXCEPTION
  WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE(SQLCODE);
  DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;


This is the BIGGEST bugs you can write in PL/SQL.
Remove it.
Read WHEN OTHERS.

[Updated on: Sat, 08 November 2014 01:04]

Report message to a moderator

Re: EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627161 is a reply to message #627146] Sat, 08 November 2014 01:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
It is Applicable for


Yes, never create tables (or any object) on the fly.
Do you delete and write, compile and link on the fly your application code each time you execute it?
No; then it is the same thing for tables.
They ARE part of the code.

Re: EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627166 is a reply to message #627144] Sat, 08 November 2014 01:32 Go to previous message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Your use of the names TEMP_EMPLPYEES and TEMP_EMPLOYESS doesn't help - re you certain that it is consistent? I am not going to go right though your code checking.

It would help if gave the actual error an the line number, would it be something like "ORA-00942: table or view does not exist" ? That will be because you have this line
INSERT INTO Temp_Employess VALUES(v_Temp_Employees(idx).EMPLOYEE_ID,   
which can;t compile if the table does not exist.

Overall, it is a terrible piece of code, for the reasons BS and MC have given and more. If this is a college homework question, you had better start again.
Previous Topic: SQL Query
Next Topic: SQL Case statement with Regular Expression
Goto Forum:
  


Current Time: Thu Apr 25 08:08:14 CDT 2024