MySQL觸發器語句在變數賦值時失敗
P粉356361722
P粉356361722 2024-04-02 15:58:14
0
1
275

以下是患者表的定義:

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

我在 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 ;

語句 SET @yearly_id = 1; 正常運作。但是如果我將其更改為 SET @yearly_id = @last_id; ,則觸發失敗。我在做這個作業時做錯了什麼? last_id有預設值,所以我需要初始化它嗎?

P粉356361722
P粉356361722

全部回覆(1)
P粉548512637

下面的程式碼可以完美運行。不同之處是 1. 變數在使用前已初始化。顯然預設值是不夠的。 1. 在「if」條件中使用了計數,而不是測試「is null」。

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 ;
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!