How do you debug MySQL stored procedures?

后端 未结 16 1254
旧时难觅i
旧时难觅i 2020-11-30 17:44

My current process for debugging stored procedures is very simple. I create a table called \"debug\" where I insert variable values from the stored procedure as it runs. Thi

16条回答
  •  再見小時候
    2020-11-30 17:56

    Answer corresponding to this by @Brad Parks Not sure about the MySQL version, but mine was 5.6, hence a little bit tweaking works:

    I created a function debug_msg which is function (not procedure) and returns text(no character limit) and then call the function as SELECT debug_msg(params) AS my_res_set, code as below:

    CREATE DEFINER=`root`@`localhost` FUNCTION `debug_msg`(`enabled` INT(11), `msg` TEXT) RETURNS text CHARSET latin1
        READS SQL DATA
    BEGIN
        IF enabled=1 THEN
        return concat('** DEBUG:', "** ", msg);
        END IF;
    END
    
    DELIMITER $$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_func_call`(
     IN RegionID VARCHAR(20),
     IN RepCurrency INT(11),
     IN MGID INT(11),
     IN VNC VARCHAR(255)
    )
    BEGIN
        SET @enabled = TRUE;
        SET @mainQuery = "SELECT * FROM Users u";
        SELECT `debug_msg`(@enabled, @mainQuery) AS `debug_msg1`;
        SET @lastQuery = CONCAT(@mainQuery, " WHERE u.age>30);
        SELECT `debug_msg`(@enabled, @lastQuery) AS `debug_msg2`;
    END $$
    DELIMITER
    

提交回复
热议问题