PostgreSQL:从表中返回select count(*);

时间:2011-01-17 14:49:22

标签: postgresql plpgsql

请再帮我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

谢谢!亚历

1 个答案:

答案 0 :(得分:4)

RETURN (select count(*) from old_ids);