source: music/triggers/customer_deletion.sql

Last change on this file was d715225, checked in by ManuelTrajcev <manueltrajcev7@…>, 11 days ago

deleted customer log trigger

  • Property mode set to 100644
File size: 811 bytes
RevLine 
[d715225]1DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
2DROP FUNCTION IF EXISTS customer_deletion();
3
[d1a8cb0]4CREATE OR REPLACE FUNCTION customer_deletion()
5RETURNS TRIGGER AS $$
[d715225]6DECLARE
7 total_spent NUMERIC(10, 2);
8 invoice_count INTEGER;
[d1a8cb0]9BEGIN
[d715225]10 SELECT COALESCE(SUM(total), 0), COUNT(*)
11 INTO total_spent, invoice_count
12 FROM invoice
13 WHERE customer_id = OLD.customer_id;
14
15 INSERT INTO deleted_customer_log (
16 first_name, last_name, deleted_at, total_spent, invoice_count
17 )
18 VALUES (
19 OLD.first_name, OLD.last_name, NOW(), total_spent, invoice_count
20 );
21
[d1a8cb0]22 RETURN OLD;
23END;
24$$ LANGUAGE plpgsql;
25
[d715225]26DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
27
[d1a8cb0]28CREATE TRIGGER trg_customer_deletion
[d715225]29BEFORE DELETE ON customer
[d1a8cb0]30FOR EACH ROW
[d715225]31EXECUTE FUNCTION customer_deletion();
Note: See TracBrowser for help on using the repository browser.