I have a before insert
trigger in a MySQL 8.0 database that checks for the incoming status and passes it along to the stored procedure, which then returns a value to be SET and passed along to be inserted. The stored procedure is being called correctly; however, when checking the entry in the database, the desired value of 1
is expected. However, NULL
is there.
Here is the BEFORE_INSERT
trigger on the table
CREATE DEFINER=`user`@`` TRIGGER `data_BEFORE_INSERT` BEFORE INSERT ON `data` FOR EACH ROW BEGIN
DECLARE newSearchAddressId INTEGER;
IF NEW.status = 'fresh' THEN
CALL CALC_FRESH_SEARCH_ADDRESS_ID(NEW.lead, @newSearchAddressId);
SET NEW.searchAddressId = @newSearchAddressId;
END IF;
END
Here is what's in the stored procedure:
CREATE DEFINER=`user`@`` PROCEDURE `CALC_FRESH_SEARCH_ADDRESS_ID`(
IN leadId INT,
OUT newSearchAddressId INT
)
proc_label: BEGIN
DECLARE lastSearchAddressID INT;
DECLARE lastStatus VARCHAR(100);
SELECT
`searchAddressId`,
`status`
INTO
lastSearchAddressID,
lastStatus
FROM `leads`
WHERE `id` = leadId;
-- if there is no status previous it's a first time entry
IF lastStatus IS NULL THEN
SET newSearchAddressId = 1;
LEAVE proc_label;
END IF;
END
In the SELECT
statement above from leads
table I am assured that only one row will be returned.
The desired outcome is that I select those two columns, status
and searchAddressId
, and if status
is NULL
, then we know that there is no previous entry and, therefore, a brand new record. Because it is a brand new record, we assign the @newSearchAddressId
to the value of 1
and leave the stored procedure early.
I think I am incorrectly setting the variables or checking in the IF statement. That's at least what I think is going on. I have looked at this post and tried setting just one variable to check with an IF statement but the same undesired result of NULL
being in the database.
Here is sample data:
DROP TABLE IF EXISTS `data`;
CREATE TABLE `data` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`lead` INT NOT NULL,
`status` VARCHAR(100) NOT NULL,
`searchAddressId INT NOT NULL
) ENGINE=INNODB;
INSERT INTO `data` (`lead`, `status`, `searchAddressId`)
VALUES(1, 'fresh', 1),
VALUES(2, 'suspended', 1),
VALUES(3, 'stale', 1),
VALUES(4, 'fresh', 1),
VALUES(5, 'cancelled', 1);
Based on the data above, here is what I am expecting from the procedure above
DECLARE lastSearchAddressID INT;
DECLARE lastStatus VARCHAR(100);
SELECT `searchAddressId`, `status`
INTO lastSearchAddressID, lastStatus
FROM `data`
WHERE `lead` = 6;
based on the SELECT
statement above searchAddressID
and lastStatus
should be NULL
so when reaching the IF
statement of
IF lastStatus IS NULL THEN
SET newSearchAddressId = 1;
LEAVE proc_label;
END IF;
Integer 1
should be assigned to the OUT newSearchAddressId INT
in the stored procedure's parameters.
A follow-up query of
SELECT * FROM `data` WHERE `lead` = 6;
Would yield:
id | lead | status | searchAddress |
---|---|---|---|
6 | 6 | 'fresh' | 1 |
What is yielded:
id | lead | status | searchAddress |
---|---|---|---|
6 | 6 | 'fresh' | NULL |
here is the db fiddle the fiddle is not working but while I fiddle with getting it working I think you can see more clearly the expectations