简体   繁体   English

如何在 SQL 中结合 WITH 语句和 INSERT INTO

[英]how to combine a WITH statement and an INSERT INTO in SQL

I have an sql statement with a with clause and I want to write the results into a table with insert into.我有一个带有 with 子句的 sql 语句,我想将结果写入带有插入的表中。 Any combination of with and insert into I tried gave me an error message.我尝试过的 with 和 insert into 的任何组合都会给我一条错误消息。 Minimal example of what I want to do:我想做的最小例子:

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
) 

Putting the with statement outside the insert into doesn't work either.将 with 语句放在 insert into 之外也不起作用。

For Oracle, you want the syntax:对于 Oracle,您需要以下语法:

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

And your CREATE TABLE statement should be:你的CREATE TABLE语句应该是:

create table temp (quarter decimal(5));

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM