Skip to content
Advertisement

PostgreSQL- If data in table, delete for stored procedure

I have this procedure, which I call with CALL pr_calc_quarter(2,2022), that insert data into a table erp.tb_quarter from a query. I need to add code to check that if there is already data in the table, to delete it before inserting new values.

I was thinking of inserting the code at the beggining or before the insert into, but I don’t know how to check if there is already data on the table.

I just need how to do the check, no need for working demo :). I don’t know if trigger before insert could work here with parameters in the procedure.

The procedure:

Advertisement

Answer

If you always want to delete all data from table then just do always

place it in next line after BEGIN

If you want to delete only data from calculated range do delete with proper where

Additionaly in place of loop you can just use INSERT INTO ... SELECT FROM construction

Example

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement