Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \connect bom
  2.  
  3. --create table bom(PartID INTEGER primary key, SuperPartID INTEGER references bom(PartID) on delete cascade, price INTEGER not null);
  4.  
  5. -- Critical operation(s): UPDATE, INSERT
  6.  
  7. CREATE OR REPLACE FUNCTION check_children_function() RETURNS trigger AS $check_children$
  8.     DECLARE
  9.         total integer;
  10.     BEGIN
  11.        
  12.         Select count(*) INTO total from bom where SuperPartID = NEW.superPartID and NEW.SuperPartID != null;
  13.         RAISE EXCEPTION 'TRIGGER GOT CALLED total was %', total;
  14.         if(total >= 5) THEN
  15.             RAISE EXCEPTION 'This part already has 5 subparts.';
  16.         END IF;
  17.         RETURN NEW;
  18.     END
  19. $check_children$ LANGUAGE plpgsql;
  20.  
  21. DROP TRIGGER check_children ON bom;
  22.  
  23. CREATE TRIGGER check_children BEFORE UPDATE OR INSERT ON bom FOR EACH ROW EXECUTE PROCEDURE check_children_function();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement