gpt4 book ai didi

sql - ORA-00942 未创建 oracle 中的 EXECUTE IMMEDIATE 临时表

转载 作者:行者123 更新时间:2023-12-01 09:25:11 29 4
gpt4 key购买 nike

基于 this answer我试图创建临时表,但是我收到异常 ORA-00942:table or view does not exist 我认为 'CREATE OR REPLACE GLOBAL TEMPORARY TABLE TempQandA( column1 number) ON COMMIT PRESERVE ROWS' 语句,它在 insert into TempQandA(column1) VALUES (1); 时失败。
请在下面找到 SQL 语句。

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;

最佳答案

动态创建GTT,所以你的INSERT也应该是动态..

请注意,PL/SQL 会在执行每个静态查询之前对其进行验证。这就是为什么即使在编译时也会出现 ORA-942 Table or view doesn't exist 错误!

因此,为了逃避这种语义检查,我们必须使调用动态化。

    BEGIN
EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE TempQandA(column1 number) ON COMMIT PRESERVE ROWS';

.....

EXECUTE IMMEDIATE ' insert into TempQandA(column1) VALUES (1)';
END;

最后,您不应该在运行时创建 GTT。以避免此类问题。无论如何,GTT 都会本地到每个 session 。

编辑:正如拉利特所说,GTTDDL 不接受 CREATE OR REPLACE

关于sql - ORA-00942 未创建 oracle 中的 EXECUTE IMMEDIATE 临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212557/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com