xdcx18
05/26/2023, 1:01 AMsql
CREATE OR REPLACE FUNCTION update_quantity(storeId bigint)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
BEGIN;
SELECT SUM(quantity) AS total_quantity
INTO total_quantity
FROM public.grocerystoreitems
WHERE "storeId" = storeId AND quantity > 0;
UPDATE grocerystores
SET quantity = total_quantity
WHERE Id = storeId;
COMMIT;
END;
$$;
https://cdn.discordapp.com/attachments/1111459092783968317/1111459093224378388/Screenshot_2023-05-25_at_7.58.09_PM.png▾
<hmmhmmhm/>
05/26/2023, 1:09 AMsql
CREATE OR REPLACE FUNCTION update_quantity(storeId bigint)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
total_quantity INTEGER;
BEGIN
SELECT SUM(quantity) AS total_quantity
INTO total_quantity
FROM public.grocerystoreitems
WHERE "storeId" = storeId AND quantity > 0;
UPDATE grocerystores
SET quantity = total_quantity
WHERE Id = storeId;
END;
$$;
xdcx18
05/26/2023, 1:15 AMhttps://cdn.discordapp.com/attachments/1111459092783968317/1111462662111510538/Screenshot_2023-05-25_at_8.15.08_PM.png▾
https://cdn.discordapp.com/attachments/1111459092783968317/1111462662417690674/Screenshot_2023-05-25_at_8.14.03_PM.png▾
garyaustin
05/26/2023, 1:15 AMxdcx18
05/26/2023, 1:17 AMsql
create or replace function update_quantity(storeid bigint)
returns void
language plpgsql as $$
declare
total_quantity numeric;
begin
select sum(quantity) as total_quantity
into total_quantity
from public.grocerystoreitems
where "storeid" = storeid and quantity > 0;
update grocerystores
set quantity = total_quantity
where id = storeid;
end;
$$;
garyaustin
05/26/2023, 1:18 AMxdcx18
05/26/2023, 1:20 AMhttps://cdn.discordapp.com/attachments/1111459092783968317/1111463949012058232/Screenshot_2023-05-25_at_8.20.07_PM.png▾
https://cdn.discordapp.com/attachments/1111459092783968317/1111463949758648391/Screenshot_2023-05-25_at_8.20.12_PM.png▾
garyaustin
05/26/2023, 1:21 AMgaryaustin
05/26/2023, 1:22 AMgaryaustin
05/26/2023, 1:31 AMsum(numeric col) as var
I don't think you also then use INTO var
after it.garyaustin
05/26/2023, 1:32 AMhttps://cdn.discordapp.com/attachments/1111459092783968317/1111466787402420224/image.png▾
xdcx18
05/26/2023, 1:33 AMhttps://cdn.discordapp.com/attachments/1111459092783968317/1111466999109922846/Screenshot_2023-05-25_at_8.31.16_PM.png▾
garyaustin
05/26/2023, 1:35 AMgaryaustin
05/26/2023, 1:35 AMgaryaustin
05/26/2023, 1:37 AMxdcx18
05/26/2023, 1:41 AMsql
create or replace function update_quantity(store int8)
returns void
language plpgsql as $$
declare
total numeric;
begin
select sum(quantity) as total
from public.grocerystoreitems
where "storeId" = store and quantity > 0;
update grocerystores
set quantity = total
where id = store;
end;
$$;
when I run this it says "Success. No rows returned". So maybe that is all the problems with the actual function, but unfortunately when I run
update_quantity(46) I keep getting invalid SQL 😔garyaustin
05/26/2023, 1:46 AMselect update....
xdcx18
05/26/2023, 1:48 AMhttps://cdn.discordapp.com/attachments/1111459092783968317/1111470877981298819/Screenshot_2023-05-25_at_8.48.01_PM.png▾
garyaustin
05/26/2023, 2:01 AMdeclare
total numeric;
begin
select sum(quantity) as total1
into total
from public.grocerystoreitems
That passes my IDE syntax checker because you are not using the same name for as and into.
But still seems funky.xdcx18
05/26/2023, 2:09 AMhttps://cdn.discordapp.com/attachments/1111459092783968317/1111476258115682374/Screenshot_2023-05-25_at_9.07.19_PM.png▾
https://cdn.discordapp.com/attachments/1111459092783968317/1111476258627399760/Screenshot_2023-05-25_at_9.07.24_PM.png▾
garyaustin
05/26/2023, 2:11 AMxdcx18
05/26/2023, 4:34 AMxdcx18
05/26/2023, 4:34 AMsql
-- Drop the trigger if it exists
DROP TRIGGER IF EXISTS update_quantity_trigger ON public.grocerystoreitems;
-- Drop the function if it exists
DROP FUNCTION IF EXISTS update_quantity();
-- Recreate the function
CREATE OR REPLACE FUNCTION update_quantity()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
total numeric;
BEGIN
RAISE NOTICE 'update_quantity called';
SELECT sum(quantity) AS total1
INTO total
FROM public.grocerystoreitems
WHERE "storeId" = NEW."storeId"
UPDATE grocerystores
SET quantity = total
WHERE id = NEW."storeId";
RETURN NEW;
END;
$$;
-- Recreate the trigger
CREATE TRIGGER update_quantity_trigger
AFTER INSERT OR UPDATE OR DELETE ON public.grocerystoreitems
FOR EACH ROW
EXECUTE FUNCTION update_quantity();
xdcx18
05/26/2023, 4:34 AMsql
-- Drop the trigger if it exists
DROP TRIGGER IF EXISTS before_delete_quantity_trigger ON public.grocerystoreitems;
-- Drop the function if it exists
DROP FUNCTION IF EXISTS before_delete_quantity();
-- Recreate the function with a new name
CREATE OR REPLACE FUNCTION before_delete_quantity()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
total numeric;
BEGIN
RAISE NOTICE 'before_delete_quantity called';
SELECT count(*) AS count1
INTO total
FROM public.grocerystoreitems
WHERE "storeId" = OLD."storeId";
IF total = 1 THEN
total := 0;
ELSE
SELECT sum(quantity) AS total1
INTO total
FROM public.grocerystoreitems
WHERE "storeId" = OLD."storeId" AND id != OLD.id;
END IF;
UPDATE grocerystores
SET quantity = total
WHERE id = OLD."storeId";
RETURN OLD;
END;
$$;
-- Recreate the trigger with the new function name
CREATE TRIGGER before_delete_quantity_trigger
BEFORE DELETE ON public.grocerystoreitems
FOR EACH ROW
EXECUTE FUNCTION before_delete_quantity();