-1

I have no idea why I can't use an INSERT INTO combined with an WITH statement:

drop table if exists testdb.with_insert_into;
create table testdb.with_insert_into (id int);

with test_data as
(select 3 id)
insert into testdb.with_insert_into (id)
select * from test_data;

I'm using version 8.0.41 of MySQL.

It outputs the error:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into testdb.with_insert_into (id) select * from test_data' at line 3

0

1 Answer 1

1

You are close. INSERT INTO needs to come first, then the WITH after. It looks and feels weird, but it is the correct order.

This works:

drop table if exists with_insert_into;
create table with_insert_into (id int);

insert into with_insert_into (id)    -- first
with test_data as                    -- second
(select 3 id)
select * from test_data;             -- finally
Sign up to request clarification or add additional context in comments.

1 Comment

I'm used to T-SQL. There, my order is the right one. Thanks for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.