Обсуждение: Stored Procedure to Delete Rows and Return Count

Поиск
Список
Период
Сортировка

Stored Procedure to Delete Rows and Return Count

От
"Dave Bolt"
Дата:

I am (unfortunately) using PG 8.4

I have created a simple stored procedure to delete records from a table but while it deletes the records it does not return the number of rows deleted.

I experimented and searched The Internet, and came up with this solution.

 

CREATE OR REPLACE FUNCTION testdel(integer) RETURNS bigint AS $$

with d as (delete from foo where id=$1 RETURNING *)

select count(*)

$$ LANGUAGE SQL;

 

Unfortunately this thinks that DELETE is a syntax error.

 

I tried the following SQL statement in phpPgAdmin (found in an answer on stackoverflow.com). This also conforms to my understanding of WITH Queries, PostgreSQL Documentation 8.4.22

 

WITH d AS (DELETE FROM foo WHERE id='1' RETURNING *) SELECT count(*);

 

and the result was a syntax error on the keyword DELETE, as above.

 

This statement was accepted as working in stackoverflow.com, but doesn't work for me.

 

Can someone give me a clue how this can be done and confirm that their solution has been tested so I know it's my end that has the problem, or better yet tell me what I'm doing wrong.

 

Thanks

Dave

Re: Stored Procedure to Delete Rows and Return Count

От
"Jonathan S. Katz"
Дата:

On Aug 29, 2018, at 5:46 PM, Dave Bolt <dave@davebolt.co.uk> wrote:

I am (unfortunately) using PG 8.4
I have created a simple stored procedure to delete records from a table but while it deletes the records it does not return the number of rows deleted.
I experimented and searched The Internet, and came up with this solution.
 
CREATE OR REPLACE FUNCTION testdel(integer) RETURNS bigint AS $$
with d as (delete from foo where id=$1 RETURNING *)
select count(*)
$$ LANGUAGE SQL;
 
Unfortunately this thinks that DELETE is a syntax error.
 
I tried the following SQL statement in phpPgAdmin (found in an answer on stackoverflow.com). This also conforms to my understanding of WITH Queries, PostgreSQL Documentation 8.4.22
 
WITH d AS (DELETE FROM foo WHERE id='1' RETURNING *) SELECT count(*);
 
and the result was a syntax error on the keyword DELETE, as above.
 
This statement was accepted as working in stackoverflow.com, but doesn't work for me.
 
Can someone give me a clue how this can be done and confirm that their solution has been tested so I know it's my end that has the problem, or better yet tell me what I'm doing wrong.

Being able to use a DELETE in a CTE was introduced in PostgreSQL 9.1.

Jonathan

Вложения

Re: Stored Procedure to Delete Rows and Return Count

От
"Jonathan S. Katz"
Дата:

On Aug 29, 2018, at 5:50 PM, Jonathan S. Katz <jonathan.katz@excoventures.com> wrote:


On Aug 29, 2018, at 5:46 PM, Dave Bolt <dave@davebolt.co.uk> wrote:

Can someone give me a clue how this can be done and confirm that their solution has been tested so I know it's my end that has the problem, or better yet tell me what I'm doing wrong.

Being able to use a DELETE in a CTE was introduced in PostgreSQL 9.1.

Borrowing from an old post[1] I made this little function:

    CREATE FUNCTION delete_stuff (y int)
    RETURNS int
    AS $$
        DECLARE
            deleted int;
        BEGIN
            DELETE FROM a WHERE x = y;
            GET DIAGNOSTICS deleted = ROW_COUNT;
            RETURN deleted;
        END
    $$ LANGUAGE plpgsql;

which returns the total # of rows deleted by the above query, which seems like
it could be adapted for your purposes.

Hope this helps,

Jonathan


Вложения

Re: Stored Procedure to Delete Rows and Return Count

От
"David G. Johnston"
Дата:
On Wednesday, August 29, 2018, Dave Bolt <dave@davebolt.co.uk> wrote:

I am (unfortunately) using PG 8.4

Then writable cte (with) is not an optibn for you.  That requires version 9.1 ROs later.

with d as (delete from foo where id=$1 RETURNING *)

select count(*)


You would have to write “from d” to get that to work but as above the delete only works in 9.1+

I would expect “get diagnostics var = row_count” (something like that) to work after executing delete by itself.  Might want to read more recent docs since the 8.4 seems to not cover this as thoroughly.


David J.

RE: Stored Procedure to Delete Rows and Return Count

От
"Dave Bolt"
Дата:

Thanks Jonathan, spot on.

Stuck with PG 8.4 at the moment on the target server. Really hope the production server will be up to date. Couldn't see any indication in the documentation that there was a difference between 9.anything and 8.4, hence the confusion.

Meanwhile, your solution is exactly what I need for now.

Dave

From: Jonathan S. Katz [mailto:jonathan.katz@excoventures.com]
Sent: 29 August 2018 22:56
To: Dave Bolt
Cc: pgsql-sql@lists.postgresql.org
Subject: Re: Stored Procedure to Delete Rows and Return Count

 

 

On Aug 29, 2018, at 5:50 PM, Jonathan S. Katz <jonathan.katz@excoventures.com> wrote:

 


On Aug 29, 2018, at 5:46 PM, Dave Bolt <dave@davebolt.co.uk> wrote:

 

Can someone give me a clue how this can be done and confirm that their solution has been tested so I know it's my end that has the problem, or better yet tell me what I'm doing wrong.

 

Being able to use a DELETE in a CTE was introduced in PostgreSQL 9.1.

 

Borrowing from an old post[1] I made this little function:

 

    CREATE FUNCTION delete_stuff (y int)

    RETURNS int

    AS $$

        DECLARE

            deleted int;

        BEGIN

            DELETE FROM a WHERE x = y;

            GET DIAGNOSTICS deleted = ROW_COUNT;

            RETURN deleted;

        END

    $$ LANGUAGE plpgsql;

 

which returns the total # of rows deleted by the above query, which seems like

it could be adapted for your purposes.

 

Hope this helps,

 

Jonathan