Guys, I can not cope with this task: I have a database of the service center, and I need to create a trigger - When adding services to the order, check that the part (spare parts) is in stock. I would be happy to help.
. That is, when adding a new “repair order” (idservice fields (service code)), we need to give an error message that the spare parts are not available. If available, the data in the repairorder table (repair order) must be filled. What so far implemented:
create function funcTrigCheck() RETURNS trigger AS ' BEGIN IF (select count(spares.quantity)<1 from spares, typesofservices, listofworks, repairorder where repairorder.idservice=listofworks.idservice and listofworks.idservice=typesofservices.idservice and typesofservices.idspares=spares.idspares) THEN RAISE EXCEPTION ''Запчастей нет!''; END IF; return null; End; ' language plpgsql; create trigger t4 before INSERT on repairorder for each row execute procedure funcTrigCheck(); How can I organize the inspection of some specific parts, what is the error in my function? That is, this function does not work correctly, count counts the total number of spare parts and displays a message.
Table "Order for repair":
create table repairorder ( idorder integer NOT NULL primary key unique, idclients integer NOT NULL REFERENCES clients(idclients), idequipment integer NOT NULL REFERENCES equipment(idequipment), idstatus integer NOT NULL REFERENCES orderstatus(idstatus), idservice integer NOT NULL REFERENCES typesofservices(idservice), sn varchar(100), disrepair varchar(100), equipment varchar(100)); Table "Types of services":
create table typesofservices ( idservice integer NOT NULL primary key unique, idspares integer NOT NULL REFERENCES spares(idspares), description varchar(100), price integer ); Spare parts table:
create table spares ( idspares integer NOT NULL primary key unique, name varchar(100), quantity integer );