PostgreSQL-将子查询与alter sequence表达式一起使用 [英] Postgresql - Using subqueries with alter sequence expressions
问题描述
我的问题很简单。
是否可以在PostgreSQL的alter expression中使用子查询?
Is it possible to use subqueries within alter expressions in PostgreSQL?
我想基于主键列值来更改序列值。
I want to alter a sequence value based on a primary key column value.
我尝试使用以下表达式,但无法执行。
I tried using the following expression, but it wouldn't execute.
更改序列public.sequenceX重新启动(从表中选择max(table_id)+1)
alter sequence public.sequenceX restart with (select max(table_id)+1 from table)
预先感谢
推荐答案
我不相信您可以那样做,但是您应该能够使用setval函数的方向,这就是alter的作用。
I don't believe you can do it like that but you should be able to use the setval function direction which is what the alter does.
select setval('sequenceX', (select max(table_id)+1 from table), false)
false将使其返回下一个序列号确切地给出了。
The false will make it return the next sequence number as exactly what is given.
这篇关于PostgreSQL-将子查询与alter sequence表达式一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!