Postgresql - 将子查询与更改序列表达式一起使用

发布于 2024-08-17 06:43:13 字数 196 浏览 2 评论 0原文

是否可以在 PostgreSQL 的 alter 表达式中使用子查询?

我想根据主键列值更改序列值。

我尝试使用以下表达式,但它不会执行。

alter sequence public.sequenceX restart with (select max(table_id)+1 from table)

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.

alter sequence public.sequenceX restart with (select max(table_id)+1 from table)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-08-24 06:43:13

我不相信你可以这样做,但你应该能够使用 setval 函数方向,这就是 alter 所做的。

select setval('sequenceX', (select max(table_id)+1 from table), false)

false 将使其返回下一个序列号,与给定的序列号完全相同。

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)

The false will make it return the next sequence number as exactly what is given.

浪推晚风 2024-08-24 06:43:13

此外,如果您有混合大小写的对象名称并且您收到如下错误:

ERROR: relation "public.mytable_id_seq" does not exist

...使用 regclass 的以下版本应该很有用:

select setval('"public"."MyTable_Id_seq"'::regclass, (select MAX("Id") FROM "public"."MyTable"))

In addition if you have mixed case object names and you're getting an error like this:

ERROR: relation "public.mytable_id_seq" does not exist

... the following version using regclass should be useful:

select setval('"public"."MyTable_Id_seq"'::regclass, (select MAX("Id") FROM "public"."MyTable"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文