How to get response message

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

How to get response message

От:
Rama Krishnan <raghuldrag@gmail.com>
Дата:
Hi All, 

I am want to delete old records using function so my senior has function like below but I want to get response of this particular inside query wheter it is successful or failure

How to get response of the function status

 create or replace function data_purge() returns void as$$
Declare
Begin
Drop table test_old;
Create table test_old as select * from sales where bill_date<now() -interval '1 year';

Delete table sales where sales_id in (select sales_id from test_old;

End;
$$ language plpgsql;

Re: How to get response message

От:
Adrian Klaver <adrian.klaver@aklaver.com>
Дата:
On 6/10/22 09:37, Rama Krishnan wrote:
> Hi All,
> 
> I am want to delete old records using function so my senior has function 
> like below but I want to get response of this particular inside query 
> wheter it is successful or failure
> 
> How to get response of the function status
> 
>   create or replace function data_purge() returns void as$$
> Declare
> Begin
> Drop table test_old;

This should probably be:

Drop table IF EXISTS test_old;

Just in case the table has already been dropped.


> Create table test_old as select * from sales where bill_date -interval '1 year';
> 
> Delete table sales where sales_id in (select sales_id from test_old;

Delete from sales where sales_id in (select sales_id from test_old);

See DELETE for more information:

https://www.postgresql.org/docs/current/sql-delete.html


As to getting execution information see:

https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS

> 
> End;
> $$ language plpgsql;
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com


Re: How to get response message

От:
Francisco Olarte <folarte@peoplecall.com>
Дата:
On Fri, 10 Jun 2022 at 18:38, Rama Krishnan  wrote:
>
> Hi All,
>
> I am want to delete old records using function so my senior has function like below but I want to get response of this particular inside query wheter it is successful or failure

> How to get response of the function status

> Drop table test_old;
> Create table test_old as select * from sales where bill_date Delete table sales where sales_id in (select sales_id from test_old;

I do a similar thing routinely and use a "move", ( insert into archive
delete from live where yadayada returning whatever ). I suppose you
could do a simiar trick.

drop table test_old; -- Beware of this, it makes your
functiondangerous, if you execute it twice you loose data.
create table test_old as delete from sales where bill_date

Re: How to get response message

От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
On Fri, Jun 10, 2022 at 9:38 AM Rama Krishnan <raghuldrag@gmail.com> wrote:
I am want to delete old records using function so my senior has function like below but I want to get response of this particular inside query wheter it is successful or failure

If it doesn't error, it was successful.  That is basically the API for a void returning function.

How to get response of the function status

 create or replace function data_purge() returns void as$$
Declare
Begin
Drop table test_old;
Create table test_old as select * from sales where bill_date<now() -interval '1 year';

Delete table sales where sales_id in (select sales_id from test_old;


Unless you are inspecting test_old outside the function you should just get rid of the table altogether.

Delete has a USING clause, I'd suggest that, with the sales subquery, instead of using IN.

If you want to return a useful count I'd move the delete into a CTE, add RETURNING, count(*) the results, and return that (changing the function output to either integer, text, or json as you desire).

David J.

FAQ