Re: Stored Procedure to Delete Rows and Return Count

"Jonathan S. Katz" <[email protected]>
Newsgroups gmane.comp.db.postgresql.sql
Message-ID <[email protected]>
> On Aug 29, 2018, at 5:46 PM, Dave Bolt <[email protected]> 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 <http://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 <http://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
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEE+oS2la8r95ogZD/x8QSccp8cZScFAluHFSUACgkQ8QSccp8c
ZSfBcA//a7Liy3J+ImjmGWRG0LuSg7TeBZfZoMwYWq89xhMeZ6eS2wpyQcAVJUKs
6O11NTkV6k3z1N/EVjPnUABZceZzMRvmrkURspbrHUSVZb7resJf2ZS2jJ3hjEaA
k4zruiC0SnP32vzwTqExAEIfpTlI94/u3lHLCgScsivyw1m9fMKTXu7Pjs1z3pX4
GONDILZOHGBV++RAfilYUy7tOn4oajVPn9eP7dRe8KSn3opXdS6KzFqLElO7cmse
6n3z7J3khcuXQQTmTDzqk5aXM/jOaeAgjjDAyu1c7k8dKQdRa884mtC+GYheEHs+
EiznkJwj+e+tKu8HOVd5lqsWAonddRAwswrTwlLoGM5qOoNmKhImKAwSso6GwpUc
7qA6usbdkZxHkaDxKkD4H3vGo3JakEmeG9opm7nXXAt8L9SMFRaqpoXaWnsZzoVw
g10hAL3Bts2FdN2IChIH+S1pOD0z7yOxHpq72U6ckQo81MWzfgN/EHtlPApYQFlx
uB/IIS/orNN6vZVLfTW8hb4q2oy5wckPgt8IAc+XMHbgcwThgZFXSfQQ7ob01A2E
VB2TUa8+/40g1pbQm0m+CTbOODS63aHE/Xo/TrunMRzp8wbFpW32I3J/bqc5E1LI
2mARCCSNcCHoYYoHXjw3WpRIFwZbmeX0HBieMcCvnLSNd5LTxzM=
=pHDN
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.