EXECUTE IMMEDIATE trowing an error in PL/SQL Block [message #627144] |
Fri, 07 November 2014 20:56 |
|
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 #627166 is a reply to message #627144] |
Sat, 08 November 2014 01:32 |
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 lineINSERT 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.
|
|
|