DreadAngel
DreadAngel

Reputation: 772

pgAdmin fails to execute an sql statement

I'm an novice in pgSQL, so I'm executing different sql commends - here is next one - I want to get count of affected rows by the last sql command:

 delete from "Menu" where "ID" = 0;
 GET DIAGNOSTICS integer_var = ROW_COUNT;
 select integer_var;

but pgAdmin says:

 ERROR:  syntax error at or near "GET"
 LINE 1: GET DIAGNOSTICS integer_var = ROW_COUNT;

what I'm doing wrong?

Upvotes: 1

Views: 2004

Answers (1)

Eelke
Eelke

Reputation: 22063

Starting with PostgreSQL 9.0 you can use DO to execute anonymous code blocks

DO $$
DECLARE
    integer_var integer;
BEGIN
    delete from "Menu" where "ID"  = 0;
    GET DIAGNOSTICS integer_var = ROW_COUNT;
    raise notice 'Rows removed: %', integer_var;
END$$;

I replaced the last select with a notice because I wasn't complete sure what you wanted to do with the result of that select and this way was the easiest to illustrate DO.

If this is in the end going to be incorporated in some program you probably have the possibility to retrieve the number of affected rows directly. libpq contains the function PQcmdTuples which returns the number of rows affected. PHP has the function pg_affected_rows and in JDBC executeUpdate returns the number of rows affected.

Upvotes: 7

Related Questions