4

I'm using Maria DB version 10.2.9 on windows 7,

MariaDB > select @@version;
+----------------+
| @@version      |
+----------------+
| 10.2.9-MariaDB |
+----------------+

I'm trying to use recursive CTE with INSERT SELECT to create some test Data. For simplicity follows below a single column table to be populated:

CREATE TABLE cte_populated
(
  id INT NOT NULL PRIMARY KEY
)
  ENGINE = InnoDB;

And the CTE which generates values 1 to 10:

WITH  RECURSIVE int_seq AS (
SELECT 1 AS val
UNION ALL
SELECT val + 1
FROM int_seq
WHERE val  < 10
)
INSERT cte_populated(id)
SELECT int_seq.val FROM int_seq;

The above generates a syntax error. Note that if the insert line is removed then the 10 rows will appear as expected with values 1 to 10 from the SELECT statement.

Does anyone know restrictions concerning use of CTE in INSERT/SELECT queries, or any workaround?

Update : The following two queries work, both the one from @elenst reply and the one from provided the link in @PM 77 comment:

INSERT cte_populated(id)
WITH  RECURSIVE int_seq AS (
SELECT 1 AS val
UNION ALL
SELECT val + 1
FROM int_seq
WHERE val  < 10)
SELECT int_seq.val as id FROM int_seq;

The following is an adaptation from the link provided by @PM 77.

INSERT INTO cte_populated
WITH  RECURSIVE int_seq(val) AS (
SELECT 1
UNION ALL
SELECT 1 + val FROM int_seq WHERE val  < 10)
SELECT * FROM int_seq;
2
  • 1
    mysqlserverteam.com/… Look for INSERT.
    – PM 77-1
    Commented Jan 29, 2018 at 23:10
  • Note that MariaDB has pseudo-tables that generate "sequences" on the fly.
    – Rick James
    Commented Feb 10, 2018 at 21:45

1 Answer 1

11

There is no restriction here, you just need to do it the other way round syntax-wise:

INSERT cte_populated(id)
WITH  RECURSIVE int_seq AS (
SELECT 1 AS val
UNION ALL
SELECT val + 1
FROM int_seq
WHERE val  < 10
)
SELECT int_seq.val FROM int_seq;

Update: to respond to the claim that it still causes the error, adding the actual client output:

MariaDB [test]> CREATE TABLE `cte_populated` (
    ->   `id` int(11) DEFAULT NULL
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.21 sec)

MariaDB [test]> INSERT cte_populated(id)
    -> WITH  RECURSIVE int_seq AS (
    -> SELECT 1 AS val
    -> UNION ALL
    -> SELECT val + 1
    -> FROM int_seq
    -> WHERE val  < 10)
    -> SELECT int_seq.val as id FROM int_seq;
Query OK, 10 rows affected (0.04 sec)
Records: 10  Duplicates: 0  Warnings: 0

MariaDB [test]> SELECT * FROM cte_populated;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
|    7 |
|    8 |
|    9 |
|   10 |
+------+
10 rows in set (0.00 sec)

MariaDB [test]> select @@version;
+----------------+
| @@version      |
+----------------+
| 10.2.9-MariaDB |
+----------------+
1 row in set (0.00 sec)
2
  • Thanks for your answer, this is also failing, see my updated question above.
    – garfield
    Commented Jan 30, 2018 at 6:18
  • If it causes a syntax error for you, you are probably not using the server version which you think you are using. Check the version and paste the whole output from the client, similar to the one that I added to the answer, but with the error that you are getting.
    – elenst
    Commented Jan 30, 2018 at 11:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.