如何将查询结果插入全局临时表?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将查询结果插入全局临时表?相关的知识,希望对你有一定的参考价值。

create global temporary table temptbl (id )

 as

  WITH t1(id, REFERENCE_ORDER_ID) AS (

  SELECT id,
         REFERENCE_ORDER_ID
  FROM   CALL_MASTER
  WHERE  REFERENCE_ORDER_ID = '1761' or id = '1761'   -- 1654 1760
  UNION ALL

  SELECT t2.id,
         t2.REFERENCE_ORDER_ID
  FROM   CALL_MASTER t2, t1
  WHERE   t2.id = t1.REFERENCE_ORDER_ID 
  ), tt(id, REFERENCE_ORDER_ID) AS (

  SELECT id,
         REFERENCE_ORDER_ID
  FROM   CALL_MASTER
  WHERE  REFERENCE_ORDER_ID = '1761' or id = '1761'   -- 1654 1760
  UNION ALL

  SELECT t2.id,
         t2.REFERENCE_ORDER_ID
  FROM   CALL_MASTER t2, tt
  WHERE   t2.REFERENCE_ORDER_ID = tt.id
  )
  --insert into temptbl values  

  insert into temptable   select * from  
 --select into temptable  select * from   
(
select distinct id  FROM   t1;
union
select distinct id  FROM   tt; 
);
--insert  into temptable  values(id); 


select * from temptbl;
答案

虽然您可以使用create-table-as-select(CTAS)语法创建全局临时表,但是将with子句放在正确的位置:

create global temporary table temptbl (id) as
with t1(id, reference_order_id) as (
  select id,
         reference_order_id
  from   call_master
  where  reference_order_id = '1761' or id = '1761'   -- 1654 1760
  union all
  select t2.id,
         t2.reference_order_id
  from   call_master t2, t1
  where  t2.id = t1.reference_order_id 
),
tt(id, reference_order_id) as (
  select id,
         reference_order_id
  from   call_master
  where  reference_order_id = '1761' or id = '1761'   -- 1654 1760
  union all

  select t2.id,
         t2.reference_order_id
  from   call_master t2, tt
  where  t2.reference_order_id = tt.id
)
select * from  
(
  select distinct id from t1
  union
  select distinct id from tt
);

创建一次GTT会更常见:

create global temporary table temptbl (id number);

然后为您的会话插入相关记录:

insert into temptbl  
with t1(id, reference_order_id) as (
  select id,
         reference_order_id
  from   call_master
  where  reference_order_id = '1761' or id = '1761'   -- 1654 1760
  union all
  select t2.id,
         t2.reference_order_id
  from   call_master t2, t1
  where   t2.id = t1.reference_order_id 
),
tt(id, reference_order_id) as (
  select id,
         reference_order_id
  from   call_master
  where  reference_order_id = '1761' or id = '1761'   -- 1654 1760
  union all

  select t2.id,
         t2.reference_order_id
  from   call_master t2, tt
  where   t2.reference_order_id = tt.id
)
select * from  
(
  select distinct id  from   t1
  union
  select distinct id  from   tt
);

Global temporary tables是永久模式对象;只有他们持有的数据是临时的 - 对于将其插入表中的会话是私有的。在运行时创建GTT表明你可能做错了,要么需要创建一个GTT作为一次性并为每个使用它的会话填充它,或者可能应该使用完全不同的机制,如PL / SQL采集。

或者你可能来自另一个RDBMS,这种模式(使用GTT或私有临时表,在Oracle版本18c之前不存在),但是你做的事情实际上并不需要Oracle中的GTT或集合。

顺便说一句,union(没有all)删除了重复项,因此你正在加入的两个查询中的distinct子句是多余的。

以上是关于如何将查询结果插入全局临时表?的主要内容,如果未能解决你的问题,请参考以下文章

mysql创建临时表,将查询结果插入已有表中

TSQL将N插入固定临时表

如何使用 jdbc oracle 插入和选择全局临时表? [复制]

mysql创建临时表,将查询结果插入已有的表

SQL 中将游标循环查询的结果插入到临时表中

如何使用 SQL 中的 Case 语句将数据插入临时表