L'instruction de déclenchement MySQL échoue lors de l'affectation de variable
P粉356361722
P粉356361722 2024-04-02 15:58:14
0
1
272

Voici la définition de la table des patients :

Patients table Columns:
PatientId int AI PK 
FirstName longtext 
LastName longtext 
Email longtext 
NIC longtext 
Phone longtext 
Address longtext 
City longtext 
Country longtext 
DOB datetime(6) 
Gender longtext 
Reference longtext 
SerialNumberYear smallint 
SerialNumber int 
DoctorId int 
CreatedOn datetime(6) 
UpdatedOn datetime(6) 
CreatedBy longtext 
SMS_Allowed tinyint(1)
Email_Allowed tinyint(1) 
SpecialConcern1 longtext 
SpecialConcern2 longtext 
SpecialConcern3 longtext 
SpecialConcern4 longtext 
CustomYearlyId longtext 
YearlyId int

J'ai le déclencheur suivant sur cette table dans MySQL :

CREATE TRIGGER generate_yearly_id BEFORE INSERT ON clinic.patients
       FOR EACH ROW
       BEGIN
           DECLARE last_id INTEGER default 10;
           DECLARE current_year DATE;
           DECLARE yearly_id INTEGER default 0;
           IF NEW.CustomYearlyId IS NULL THEN
           BEGIN
                INSERT INTO DEBUG VALUES (null,"start trigger");
                 #SET @last_id := (SELECT max(yearlyid) FROM patients WHERE 
                 #      YEAR(createdon)=YEAR(CURDATE()));
                IF last_id IS NOT NULL THEN
                BEGIN
                    SET @yearly_id = 1;
                    INSERT INTO DEBUG VALUES (null, concat("in if lastid is not null ",@yearly_id));
                END;
                ELSE
                BEGIN
                    #SET @yearly_id := 1;
                END;
                END IF;
                SET NEW.yearlyid := @yearly_id;
           END;
           END IF;
       END;//
       delimiter ;

déclaration SET @yearly_id = 1; 工作正常。但是如果我将其更改为 SET @yearly_id = @last_id; , le déclencheur échoue. Qu’ai-je fait de mal en effectuant ce devoir ? last_id a une valeur par défaut, dois-je donc l'initialiser ?

P粉356361722
P粉356361722

répondre à tous(1)
P粉548512637

Le code ci-dessous fonctionne parfaitement. La différence est de 1. La variable a été initialisée avant utilisation. Apparemment, la valeur par défaut n'est pas suffisante. 1. Le nombre utilisé dans la condition « si » au lieu de tester « est nul ».

CREATE TRIGGER generate_yearly_id BEFORE INSERT ON clinic.patients
       FOR EACH ROW
       BEGIN
           DECLARE last_id INTEGER default 0;
           DECLARE current_year DATE;
           DECLARE yearly_id INTEGER default 1;
           declare count INTEGER default 0;
           IF NEW.CustomYearlyId IS NULL THEN
           BEGIN
                #init last_id to some value before using
                SET @last_id = 0;
                SET @yearly_id = 0;
                #INSERT INTO DEBUG VALUES (null,"start trigger");
                set @count := (select count(*) from patients where
                        YEAR(CreatedOn)=YEAR(curdate()));
                IF @count > 0 THEN
                BEGIN
                    SET @last_id := (SELECT max(yearlyid) FROM patients WHERE 
                       YEAR(createdon)=YEAR(CURDATE()));
                    SET @yearly_id = @last_id + 1;
                    SET NEW.yearlyid := @yearly_id;
                    #INSERT INTO DEBUG VALUES (null, concat("in if lastid is not null ",@yearly_id));
                END;
                ELSE
                BEGIN
                    #INSERT INTO DEBUG VALUES (null, concat("in else ",@yearly_id));
                    #SET @yearly_id = 1;
                    SET NEW.yearlyid := 1;
                END;
                END IF;
                #Set NEW.yearlyid := 1;
           END;
           END IF;
       END;//
       delimiter ;
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!