【问题标题】:how to combine a WITH statement and an INSERT INTO in SQL如何在 SQL 中组合 WITH 语句和 INSERT INTO
【发布时间】:2021-07-26 19:35:22
【问题描述】:

我有一个带有 with 子句的 sql 语句,我想用 insert into 将结果写入一个表中。我尝试过的 with 和 insert into 的任何组合都会给我一条错误消息。我想做的最小例子:

create table temp as (quarter decimal(5));

insert into temp (
with bla as (select 2021 as year from dummy)
select 10*year + 1 as quarter from bla
) 

将 with 语句放在 insert into 之外也不起作用。

【问题讨论】:

    标签: sql oracle sql-insert common-table-expression


    【解决方案1】:

    对于 Oracle,您需要以下语法:

    insert into temp (quarter)
    with bla (year) as (
      select 2021 as year from dual
    )
    select 10*year + 1 as quarter from bla;
    

    你的CREATE TABLE 声明应该是:

    create table temp (quarter decimal(5));
    

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 2015-02-21
      • 2021-09-22
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多