PostgreSQL 函数中的 ALTER SEQUENCE
我有触发功能:
CREATE OR REPLACE FUNCTION update_aaa() RETURNS TRIGGER AS $$
DECLARE maxid INTEGER;
BEGIN
SELECT MAX(id) INTO maxid FROM aaa;
ALTER SEQUENCE aaa_id_seq RESTART WITH maxid;
END;
$$ LANGUAGE plpgsql;
并且有错误:
ERROR: syntax error at or near "$1"
Line 1: ALTER SEQUENCE aaa_id_seq RESTART WITH $1
为什么 $1 ?
什么错误?
I have trigger function:
CREATE OR REPLACE FUNCTION update_aaa() RETURNS TRIGGER AS $
DECLARE maxid INTEGER;
BEGIN
SELECT MAX(id) INTO maxid FROM aaa;
ALTER SEQUENCE aaa_id_seq RESTART WITH maxid;
END;
$ LANGUAGE plpgsql;
And have error:
ERROR: syntax error at or near "$1"
Line 1: ALTER SEQUENCE aaa_id_seq RESTART WITH $1
Why $1 ?
What error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许使用
setval
函数而不是改变序列...重新启动
?Maybe use the
setval
function rather thanalter sequence ... restart with
?我认为你需要使用 EXECUTE 用于 PL/pgSQL 中的数据定义命令(如 ALTER)。并且在计算 MAX(id) 之前需要
LOCK TABLE aaa IN SHARE MODE;
,以防止表数据并发更改。I think you need to use EXECUTE for data definition commands (like ALTER) in PL/pgSQL. And you need to
LOCK TABLE aaa IN SHARE MODE;
before calculating MAX(id) to prevent concurrent changes to table data.您的表可能是空的,因此
返回
NULL
;将查询更改为
Your table is probably empty so
returns
NULL
;Change the query to