简体   繁体   中英

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. Any combination of with and insert into I tried gave me an error message. 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.

For Oracle, you want the syntax:

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 temp (quarter decimal(5));

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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