问题
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.
回答1:
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 this:
select * from all_objects where object_name = 'TEMP_TRAN';
回答2:
IF v_Exists = 1 THEN
EXECUTE IMMEDIATE "DROP TABLE TEMP_TRAN";
ENDIF
the only problem with this procedure is that when the table you are looking for doesn't exist then trouble began with a no data found error and you'll not even get into that line that check for v exists value. :)
回答3:
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.
来源:https://stackoverflow.com/questions/2692140/oracle-can-we-create-global-temp-tables-or-any-tables-in-stored-proc