workbook to create triggers and procedures

fernand.st-georges@protonmail.com's profile image fernand.st-georges@protonmail.com posted 3 years ago in General Permalink

Hi I'm looking forward to figure if it is better to create a StoredProcedure against a trigger. I haven't found how to create triggers as I used to in SQL Server

LIKE THIS EXAMPLE

USE [LIGUE_PTI_QUEBEC] GO /** Object: Trigger [dbo].[CREER_SOMMAIRE_PARTIE_JOUEUR] Script Date: 08/févr./2021 13:07:36 **/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER TRIGGER [dbo].[CREER_SOMMAIRE_PARTIE_JOUEUR] ON [dbo].[SOMMAIRE_PARTIE] FOR INSERT as

DECLARE @NB_BUT int, @RESULTAT_VISITEUR int, @ID_EQUIPE_VISITEUR int, @ID_EQUIPE_LOCAL int, @ID_EQUIPE_JOUEUR int, @RESULTAT_LOCAL int, @NB_PASSE int, @NB_MINUTE_PUNITION int, @NB_BUT_TOTAL_JOUEUR int, @NB_PASSE_TOTAL_JOUEUR int, @NB_MINUTE_PUNITION_JOUEUR int

/ Sélectionner le nombre de but inscrit par un joueur / SELECT @NB_BUT = NB_BUT FROM inserted SOMMAIRE_PARTIE

/ Sélectionner le résultat avant tout changement de l'équipe visiteur / SELECT @RESULTAT_VISITEUR = RESULTAT_VISITEUR FROM PARTIE join SOMMAIRE_PARTIE on PARTIE.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE join inserted ON inserted.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE

/ Sélectionner le résultat avant tout changement de l'équipe locale / SELECT @RESULTAT_LOCAL = RESULTAT_LOCAL FROM PARTIE join SOMMAIRE_PARTIE on PARTIE.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE join inserted ON inserted.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE

/ Inscrire le résultat de l'équipe visiteur / UPDATE PARTIE SET RESULTAT_VISITEUR = @NB_BUT + @RESULTAT_VISITEUR FROM PARTIE JOIN EQUIPE ON PARTIE.ID_EQUIPE_VISITEUR = EQUIPE.ID_EQUIPE JOIN JOUEUR ON EQUIPE.ID_EQUIPE = JOUEUR.ID_EQUIPE join SOMMAIRE_PARTIE ON JOUEUR.ID_JOUEUR = SOMMAIRE_PARTIE.ID_JOUEUR AND PARTIE.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE join inserted ON inserted.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE AND inserted.ID_JOUEUR = SOMMAIRE_PARTIE.ID_JOUEUR

/ Inscrire le résultat de l'équipe locale / UPDATE PARTIE SET RESULTAT_LOCAL = @NB_BUT + @RESULTAT_LOCAL FROM PARTIE JOIN EQUIPE ON PARTIE.ID_EQUIPE_LOCALE = EQUIPE.ID_EQUIPE JOIN JOUEUR ON EQUIPE.ID_EQUIPE = JOUEUR.ID_EQUIPE join SOMMAIRE_PARTIE ON JOUEUR.ID_JOUEUR = SOMMAIRE_PARTIE.ID_JOUEUR AND PARTIE.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE join inserted ON inserted.ID_PARTIE = SOMMAIRE_PARTIE.ID_PARTIE AND inserted.ID_JOUEUR = SOMMAIRE_PARTIE.ID_JOUEUR

fernand.st-georges@protonmail.com's profile image fernand.st-georges@protonmail.com posted 3 years ago Permalink

I found the solution

CREATE TRIGGER ligne_bon_commande_after_update AFTER UPDATE ON ligne_bon_commande FOR EACH ROW DECLARE V_cout_total FLOAT; DECLARE V_total_commande FLOAT;

SELECT OLD.cout_total INTO V_cout_total; SELECT OLD.total_commande INTO v_total_commande FROM bon_commande WHERE bon_commande.ID_BON_COMMANDE = new.ID_BON_COMMANDE; UPDATE bon_commande SET bon_commande.total_commande = v_cout_total + v_total_commande WHERE bon_commande.ID_BON_COMMANDE = new.ID_BON_COMMANDE;

set new.cout_total = new.quantite_commandee * new.prix_unitaire_paye; END

1 attachment(s):
  • decle
fernand.st-georges@protonmail.com's profile image fernand.st-georges@protonmail.com posted 3 years ago Permalink

I got a similar example, as I changed the logic. In this case, I inserted a value for Quantite_commandee, for which is a value for the quantity ordered. I did not know the price at that time, normal when you create a purchase order. Normaly when the order is received, you also receive the price. So the update comes related to the quantity received with the price. What if an element is BO, well the your update is related with an IF THEN AND ELSE CLAUSE.

CREATE DEFINER=root@localhost TRIGGER ligne_bon_commande_before_update BEFORE UPDATE ON ligne_bon_commande FOR EACH ROW BEGIN DECLARE V_quantite_RECUE FLOAT; DECLARE V_prix_unitaire_paye FLOAT; DECLARE V_cout_total FLOAT; DECLARE V_NEW_total_commande FLOAT; DECLARE V_OLD_total_commande FLOAT;

IF OLD.QUANTITE_RECUE <> NEW.QUANTITE_RECUE THEN SET V_quantite_RECUE = NEW.QUANTITE_RECUE; ELSE SET V_quantite_RECUE = OLD.QUANTITE_RECUE; END IF;

IF OLD.PRIX_UNITAIRE_PAYE <> NEW.PRIX_UNITAIRE_PAYE THEN SET V_prix_unitaire_paye = NEW.PRIX_UNITAIRE_PAYE; ELSE SET V_prix_unitaire_paye = new.PRIX_UNITAIRE_PAYE; END IF;

set new.cout_total = V_quantite_RECUE * V_prix_unitaire_paye;

UPDATE bon_commande SET bon_commande.total_commande = NEW.cout_total WHERE bon_commande.ID_BON_COMMANDE = new.ID_BON_COMMANDE; END

Please login to leave a reply, or register at first.