问题
Based on this answer I was trying to create temp table, however I am getting exception ORA-00942:table or view does not exist
I would assume there is something wrong with 'CREATE OR REPLACE GLOBAL TEMPORARY TABLE TempQandA(column1 number) ON COMMIT PRESERVE ROWS'
statement, it fails on insert into TempQandA(column1) VALUES (1);
.
Please find SQL statement below.
DECLARE
TransactioDetailId numeric := 3132;
HomePhoneNumber varchar(20);
MobileNumber varchar(20);
Email varchar(20);
whatever varchar(20);
BEGIN
EXECUTE IMMEDIATE 'CREATE OR REPLACE GLOBAL TEMPORARY TABLE TempQandA(column1 number) ON COMMIT PRESERVE ROWS';
BEGIN
SELECT contactvalue into HomePhoneNumber FROM customercontact CC
inner join TRANSACTIONMAIN tm on tm.customerid = CC.customerid
inner join transactiondetail td on td.transactionid = tm.transactionid
where contacttypeid = 2 and td.transactiondetailid = TransactioDetailId;
EXCEPTION
WHEN NO_DATA_FOUND THEN
HomePhoneNumber := NULL;
begin
SELECT contactvalue into MobileNumber FROM customercontact CC
inner join TRANSACTIONMAIN tm on tm.customerid = CC.customerid
inner join transactiondetail td on td.transactionid = tm.transactionid
where contacttypeid = 3 and td.transactiondetailid = TransactioDetailId;
EXCEPTION
WHEN NO_DATA_FOUND THEN
MobileNumber := NULL;
begin
SELECT contactvalue into Email FROM customercontact CC
inner join TRANSACTIONMAIN tm on tm.customerid = CC.customerid
inner join transactiondetail td on td.transactionid = tm.transactionid
where contacttypeid = 1 and td.transactiondetailid = TransactioDetailId;
EXCEPTION
WHEN NO_DATA_FOUND THEN
Email := NULL;
begin
insert into TempQandA(column1) VALUES (1);
end;
end;
end;
end;
END;
回答1:
You dynamically create the GTT
, so your INSERT
too should be dynamic..
Note that, PL/SQL validates every static query before even executing it.
Thats why you get ORA-942 Table or view doesn't exist
error even at compilation time!
So, to escape this semantic check, we have to make the call dynamic.
BEGIN
EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE TempQandA(column1 number) ON COMMIT PRESERVE ROWS';
.....
EXECUTE IMMEDIATE ' insert into TempQandA(column1) VALUES (1)';
END;
And Finally, you should not be creating the GTT
on run time.. To avoid such issues. The GTT is anyway going to local to every session.
EDIT: As Lalit says, GTT
's DDL
doesn't accept CREATE OR REPLACE
回答2:
'CREATE OR REPLACE GLOBAL TEMPORARY TABLE TempQandA(column1 number) ON COMMIT PRESERVE ROWS';
The keyword REPLACE
is incorrect. You need to simply create the GTT
and insert the values into it using EXECUTE IMMEDIATE
:
SQL> BEGIN
2 EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE Temp_gtt(column1 number) ON COMMIT PRESERVE ROWS';
3 EXECUTE IMMEDIATE 'insert into temp_gtt(column1) values(1)';
4 END;
5 /
PL/SQL procedure successfully completed.
SQL>
SQL> select * from temp_gtt;
COLUMN1
----------
1
SQL>
来源:https://stackoverflow.com/questions/26212557/execute-immediate-temp-table-in-oracle-does-not-get-created-ora-00942