magicbyt3
04/09/2022, 10:29 AMsql
UPDATE TABLE_A
SET col_a = CASE WHEN col_a + val_a < 11
THEN col_a + val_a
ELSE col_a END
WHERE userid = auth.uid();
IF FOUND THEN
UPDATE TABLE_B
SET col_b = col_b - val_b
WHERE id = auth.uid();
END IF;
TABLE_A is updated only if col_a + val_a < 11 however TABLE_B always gets updated and I only want to update it if TABLE_A has been updated first. How would I do that?
Thanks in advancetourdownunder
04/10/2022, 12:25 AMsql
DECLARE
row_a TABLE_A;
BEGIN
UPDATE TABLE_A
SET col_a = CASE WHEN col_a + val_a < 11
THEN col_a + val_a
ELSE col_a END
WHERE userid = auth.uid()
RETURNING * INTO row_a;
IF row_a THEN
UPDATE TABLE_B
SET col_b = col_b - val_b
WHERE id = row_a.userid;
END IF;
END;