【问题标题】:How to insert CTE output to temp table in Oracle?如何将 CTE 输出插入 Oracle 中的临时表?
【发布时间】:2018-05-04 06:19:30
【问题描述】:

在 Oracle 中,我尝试使用以下代码将 CTE 的输出插入到临时表中,但它给出了错误。我不想提前创建临时表,它应该使用 CTE 中的列名和数据类型动态创建。

with cte as (
select ORDER_ID, STATUS_ID, CALL_DATE, SHIP_DATE,
       UPDATE_USER_ID,  UPDATE_TIMESTAMP,
       row_number() over(partition by ORDER_ID order by update_timestamp desc) as rowno 
FROM ORDER_HISTORY
where ORDER_ID in (1001,1002, 1003)
)
create table temp_recent_order as
select * from cte where rowno=1

【问题讨论】:

    标签: sql oracle common-table-expression ddl


    【解决方案1】:

    只需替换create table 语句的顺序,如下所示:

    create table temp_recent_order as
    with cte as (
    select ORDER_ID,
           STATUS_ID,
           CALL_DATE,
           SHIP_DATE,
           UPDATE_USER_ID,
           UPDATE_TIMESTAMP,
           row_number() over(partition by ORDER_ID order by update_timestamp desc) as rowno
      from ORDER_HISTORY
     where ORDER_ID in (1001, 1002, 1003)
    )
    select * from cte where rowno=1;
    

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 2018-07-06
      • 2015-11-27
      • 2018-01-13
      • 2018-06-26
      • 2018-09-03
      相关资源
      最近更新 更多