0

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

Ctfrancia
  • 1,248
  • 1
  • 15
  • 39
  • 1
    you are confusing user defined variables and local variables @leadId is not the same as leadid – P.Salmon Nov 29 '22 at 10:11
  • @P.Salmon thanks, so the issue is within the stored procedure and how I am using the local vs. user defined variables? – Ctfrancia Nov 29 '22 at 10:46
  • `DECLARE lastStatus VARCHAR(100);` is local variable. `@lastStatus` is user-defined variable. ANOTHER variable. Assigning the value to one of them does not alter the value of another variable. Recommendation - do not use UDV, use only LV. – Akina Nov 29 '22 at 10:49
  • ok @Akina thanks, I will look at it further, I have changed it to only use LV as per your suggestion however, entries in the column are still `null` so I'll keep investigating, but thanks for pointing me in the right direction – Ctfrancia Nov 29 '22 at 11:00
  • Provide some sample data (CREATE TABLE + INSERT INTO, 3-5 rows), INSERT query and desired final data state. – Akina Nov 29 '22 at 11:04
  • @Akina I hope the data is sufficient. – Ctfrancia Nov 29 '22 at 11:37
  • *I hope the data is sufficient.* No. Your SP calls from `leads` table - you'd provide it too. PS. A fiddle with your data and code: https://dbfiddle.uk/QgQiWm4G You may edit/update it and provide the link. – Akina Nov 29 '22 at 13:04

0 Answers0