I have tried to run this query:
CREATE Function sp_test_case () returns void as $$
DECLARE
cont int=(Select MAX(id_fact)from backup_factura);
BEGIN
while cont>0
LOOP
UPDATE backup_factura
SET tipo= CASE
WHEN ((total_fact) <=100) THEN 'X'
WHEN ((total_fact) <=200) THEN 'Y'
ELSE 'Z'
END;
OUTPUT Deleted.tipo AS BeforeValue,
Inserted.tipo AS AfterValue
WHERE id_fact=cont;
cont:=cont-1;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;
I adapted the code that was originally written in SQL server. Now my doubt is: Do you know the equivalent of the OUTPUT statement?
The OUTPUT clause is used to display the before and after vacation values. F.E:
OUTPUT Deleted.BusinessEntityID, Deleted.VacationHours AS BeforeValue,
Inserted.VacationHours AS AfterValue
Any suggestion of what do I need to do in order to make it work?
Thanks in advance for your time & support!
OUTPUT Deleted.tipo AS BeforeValueoption in Postgres. Are you looking for thereturningclause? postgresql.org/docs/current/static/sql-update.html You also didn't declare theBeforevalueandAfterValuevariables. And you are not using them anyways. What exactly are you trying to achieve? As far as I can tell, you can simply use:UPDATE backup_factura SET tipo = CASE WHEN total_fact <=100 THEN 'X' WHEN total_fact <=200 THEN 'Y' ELSE 'Z' END CASE;no need for the loop no need for thewhereclause. That will be much faster.outputclause in Postgres. Check the manual (you are also ending theupdatestatement with an;then you have the (invalid)outputclause with awhereclause that doesn't make sense.