PostgreSQL:从表中返回选择计数(*);
请帮我解决另外 1 个 PL/pgSQL 问题。
我有一个 PHP 脚本作为每日 cronjob 运行,并从 1 个主表和几个引用其“id”列的其他表中删除旧记录:
create or replace function quincytrack_clean()
returns integer as $BODY$
begin
create temp table old_ids
(id varchar(20)) on commit drop;
insert into old_ids
select id from quincytrack
where age(QDATETIME) > interval '30 days';
delete from hide_id where id in
(select id from old_ids);
delete from related_mks where id in
(select id from old_ids);
delete from related_cl where id in
(select id from old_ids);
delete from related_comment where id in
(select id from old_ids);
delete from quincytrack where id in
(select id from old_ids);
return select count(*) from old_ids;
end;
$BODY$ language plpgsql;
以下是我从 PHP 脚本中调用它的方法:
$sth = $pg->prepare('select quincytrack_clean()');
$sth->execute();
if ($row = $sth->fetch(PDO::FETCH_ASSOC))
printf("removed %u old rows\n", $row['count']);
为什么会出现以下错误?
SQLSTATE[42601]: Syntax error: 7
ERROR: syntax error at or near "select" at character 9
QUERY: SELECT select count(*) from old_ids
CONTEXT: SQL statement in PL/PgSQL function
"quincytrack_clean" near line 23
谢谢你!亚历克斯
please help me with 1 more PL/pgSQL question.
I have a PHP-script run as daily cronjob and deleting old records from 1 main table and few further tables referencing its "id" column:
create or replace function quincytrack_clean()
returns integer as $BODY$
begin
create temp table old_ids
(id varchar(20)) on commit drop;
insert into old_ids
select id from quincytrack
where age(QDATETIME) > interval '30 days';
delete from hide_id where id in
(select id from old_ids);
delete from related_mks where id in
(select id from old_ids);
delete from related_cl where id in
(select id from old_ids);
delete from related_comment where id in
(select id from old_ids);
delete from quincytrack where id in
(select id from old_ids);
return select count(*) from old_ids;
end;
$BODY$ language plpgsql;
And here is how I call it from the PHP script:
$sth = $pg->prepare('select quincytrack_clean()');
$sth->execute();
if ($row = $sth->fetch(PDO::FETCH_ASSOC))
printf("removed %u old rows\n", $row['count']);
Why do I get the following error?
SQLSTATE[42601]: Syntax error: 7
ERROR: syntax error at or near "select" at character 9
QUERY: SELECT select count(*) from old_ids
CONTEXT: SQL statement in PL/PgSQL function
"quincytrack_clean" near line 23
Thank you! Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)