tags:

views:

285

answers:

2

Hi,

below is the stored proc I wrote:

create or replace procedure test005
as
begin

CREATE GLOBAL TEMPORARY TABLE TEMP_TRAN 
( 
COL1 NUMBER(9), 
COL2 VARCHAR2(30), 
COL3 DATE 
) ON COMMIT PRESERVE ROWS 

/
INSERT INTO TEMP_TRAN VALUES(1,'D',sysdate); 

INSERT INTO TEMP_TRAN VALUES(2,'I',sysdate); 

INSERT INTO TEMP_TRAN VALUES(3,'s',sysdate); 

COMMIT;
end;

when i executed it , i get an error message mentioning:

create or replace procedure test005
as
begin

CREATE GLOBAL TEMPORARY TABLE TEMP_TRAN 
( 
COL1 NUMBER(9), 
COL2 VARCHAR2(30), 
COL3 DATE 
) ON COMMIT PRESERVE ROWS 

/
INSERT INTO TEMP_TRAN VALUES(1,'D',sysdate); 

INSERT INTO TEMP_TRAN VALUES(2,'I',sysdate); 

INSERT INTO TEMP_TRAN VALUES(3,'s',sysdate); 

COMMIT;
end;
Error at line 1
ORA-00955: name is already used by an existing object

Script Terminated on line 1.

I tried to drop the TEMP_TRAN and it says table doesn't exist. So there is no TEMP_TRAN table existed in system. why am I getting this error? I am using TOAD to create this stored proc.

Any help would be highly appreciated.

+4  A: 

Global temporary tables are not meant to be created "on the fly" by stored procedures. They are to be created once, permanently, like any other table. It is the data that is temporary, not the table object.

Regarding TEMP_TRAN, perhaps an object of that name exists, but is not a table? Try:

select * from all_objects where object_nane = 'TEMP_TRAN';
Tony Andrews
A: 

Have you tried this query?

select * from all_tables where table_name like '%TEMP_TRAN%'

If so, is the table listed?

Another way to go is by using the EXECUTE IMMEDIATE statement.

CREATE OR REPLACE PROCEDURE P_TEST005 AS
v_Exists NUMBER;
BEGIN
    v_Exists := 0;
    SELECT 1 INTO v_Exists
        FROM ALL_TABLES
        WHERE TABLE_NAME LIKE '%TEMP_TRAN%';

    IF v_Exists = 1 THEN
        EXECUTE IMMEDIATE "DROP TABLE TEMP_TRAN";
    ENDIF

    EXECUTE IMMEDIATE 
        "CREATE GLOBAL TEMPORARY TABLE TEMP_TRAN (
            COL1 NUMBER(9), 
            COL2 VARCHAR2(30), 
            COL3 DATE
        ) ON COMMIT PRESERVE ROWS"

    EXCEPTION
        WHEN OTHERS THEN 
            NULL;
END;

Please consider my rusty Oracle skills. I haven't worked with Oracle for about 2 years now. But if you work your way around it, you might achieve what you're trying to do.

However, I do agree that a temporary table is not meant to be dropped procedurely, but rather to exist just like a regular table.

Will Marcouiller
No need to "work around" the problem - as you say, temporary tables should NEVER be created or dropped procedurally. (you almost got a -1 from me ;)
Jeffrey Kemp
I agree, but sometimes one wishes to make something in a way for some reasons we are not aware of. Even though it is not a best practice or so to make something in a particular way, one is free to do whatever he wishes in order to achieve what he wants. So, I give the solution for what he wants to do, advising him that it is not a recommended practice. Thanks anyway for not downvoting my answer.
Will Marcouiller