MariaDB 11.x - ALTER SEQUENCE RESTART WITH not working?

18 Views Asked by At

I'm trying to reset my sequences using the ALTER SEQUENCE that says it's supported in MariaDB 10.3+ since they introduced sequence support.

But I'm getting an error trying to restart my sequences using the MAX(id) + 1 value of the table the sequence is for.

According to the help for MariaDB it looks like it should be supported, but I'm getting a syntax error.

ALTER SEQUENCE Help - https://mariadb.com/kb/en/alter-sequence/

Any help is appreciated..

MariaDB [bcfactory]> select * from job_SEQ;
+-----------------------+---------------+---------------------+-------------+-----------+------------+--------------+-------------+
| next_not_cached_value | minimum_value | maximum_value       | start_value | increment | cache_size | cycle_option | cycle_count |
+-----------------------+---------------+---------------------+-------------+-----------+------------+--------------+-------------+
|                     1 |             1 | 9223372036854775806 |           1 |        50 |       1000 |            0 |           0 |
+-----------------------+---------------+---------------------+-------------+-----------+------------+--------------+-------------+
1 row in set (0.000 sec)

MariaDB [bcfactory]> alter sequence job_SEQ RESTART WITH (select MAX(id) + 1 from job);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(select MAX(id) + 1 from job)' at line 1
MariaDB [bcfactory]> select version();
+---------------------------------------+
| version()                             |
+---------------------------------------+
| 11.2.2-MariaDB-1:11.2.2+maria~ubu2204 |
+---------------------------------------+
1 row in set (0.000 sec)
1

There are 1 best solutions below

1
ysth On

No, you cannot just use a subquery where it expects a value. You need to use dynamic sql for this:

set @restart_sql:=concat('alter sequence job_SEQ restart with ',(select MAX(id) + 1 from job));
prepare restart_sequence from @restart_sql;
execute restart_sequence;
deallocate prepare restart_sequence;

fiddle