而PostgreSQL 9.3手册介绍了此命令如何执行PostgreSQL的GET诊断“
'GET DIAGNOSTICS int_var = ROW_COUNT;'
从去年insert/update
命令获取受影响的行数。 当我尝试这样的GUI(pgAdmin的3,SQL编辑器面板),并在Python(使用psycopg2包)我得到这个错误:
ERROR: syntax error at or near "GET".
我在做什么错?
而PostgreSQL 9.3手册介绍了此命令如何执行PostgreSQL的GET诊断“
'GET DIAGNOSTICS int_var = ROW_COUNT;'
从去年insert/update
命令获取受影响的行数。 当我尝试这样的GUI(pgAdmin的3,SQL编辑器面板),并在Python(使用psycopg2包)我得到这个错误:
ERROR: syntax error at or near "GET".
我在做什么错?
它是后端功能,不是SQL语言的一部分。 Psycopg2具有包含此值的cursor.rowcount属性。 Libpq C/C++库具有PQcmdTuples函数。
GET DIAGNOSTICS只能在plpgsql函数中使用。你不能像普通的sql命令那样执行它。请参阅以下简单示例:
create table fruits as
select * from (values
(1, 'banana'),
(2, 'pear'),
(3, 'apple')) f(id, fruit);
create function update_fruits()
returns setof fruits language plpgsql
as $$
declare
n int;
begin
update fruits set id = id+ 1;
get diagnostics n = row_count;
return query select null::int, format ('%s rows updated', n);
return query select * from fruits order by 1;
get diagnostics n = row_count;
return query select null::int, format ('%s rows retrieved', n);
end $$;
select * from update_fruits()
id | fruit
----+------------------
| 3 rows updated
2 | banana
3 | pear
4 | apple
| 3 rows retrieved
(5 rows)
有时最简单的解决方案是最好的... cursor.rowcount很好。 –