Search code examples
mysqlstored-proceduresprocedure

I don't know what's wrong with my MYSQL procedure


to try to create a procedure in MYSQL workbench but I'm not succeeding ..

A procedure inserts into a table with parameters coming from an ASP program, inserts into the campaign table, then by the inserted ID, inserts into another table and returns the inserted id from that table in the last table.

What am I doing wrong? I'm getting used to SQL Server....

CREATE PROCEDURE Insert_Campaign_Indicator(
     IN Name VARCHAR(50),
     IN Email VARCHAR(50),
     IN Phone VARCHAR(50),
     IN Active INT,
     IN Type INT,
     IN UserId INT,
     IN CampaignId INT
)

BEGIN
    INSERT INTO Indicator(Name, Email, Phone, Link, Active, CleaningType, Type, UserId) 
                                VALUES (Name, Email, Phone, uuid(), Active, 2, Type, UserId);

        INSERT INTO CampaignIndicator (CampaignId, IndicatorId, Link, ResearchWasSent, ReadyToRefer, AcceptedRefer, Active, UserId)
            VALUES (CampaignId, LAST_INSERT_ID(), uuid(),0,0,0, 1, UserId);

        SELECT Link FROM CampaignIndicator WHERE Id = LAST_INSERT_ID();
END //

DELIMITER ;

Solution

  • Never use Column names as variable, MySQL gets confused

    The code is without DELIMITER because of the dbfddle site you have to add them

    CREATE TABLE Indicator(id int AUTO_INCREMENT PRIMARY KEY,Name VARCHAR(50)
    , Email VARCHAR(50), Phone VARCHAR(50), Link VARCHAR(36),Active Int, CleaningType int, Type int, UserId int)
    
    CREATE TABLE CampaignIndicator (id int AUTO_INCREMENT PRIMARY KEY,CampaignId int
    , IndicatorId int, Link VARCHAR(36), ResearchWasSent int, ReadyToRefer int, AcceptedRefer int
    , Active int, UserId int)
    
    CREATE PROCEDURE Insert_Campaign_Indicator(
         IN _Name VARCHAR(50),
         IN _Email VARCHAR(50),
         IN _Phone VARCHAR(50),
         IN _Active INT,
         IN _Type INT,
         IN _UserId INT,
         IN _CampaignId INT
    )
    
    BEGIN
        INSERT INTO Indicator(Name, Email, Phone, Link, Active, CleaningType, Type, UserId) 
                                    VALUES (_Name, _Email, _Phone, uuid(), _Active, 2, _Type, _UserId);
    
            INSERT INTO CampaignIndicator (CampaignId, IndicatorId, Link, ResearchWasSent, ReadyToRefer, AcceptedRefer, Active, UserId)
                VALUES (_CampaignId, LAST_INSERT_ID(), uuid(),0,0,0, 1, _UserId);
    
            SELECT Link FROM CampaignIndicator WHERE Id = LAST_INSERT_ID();
    END
    
    CALL Insert_Campaign_Indicator('A','B','C',1,1,1,1)
    
    | Link                                 |
    | :----------------------------------- |
    | 28aee8e1-d169-11eb-96e0-00163e64f9cc |
    
    ✓
    

    db<>fiddle here