How do you debug MySQL stored procedures?

后端 未结 16 1282
旧时难觅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 18:09

    The following debug_msg procedure can be called to simply output a debug message to the console:

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `debug_msg`$$
    DROP PROCEDURE IF EXISTS `test_procedure`$$
    
    CREATE PROCEDURE debug_msg(enabled INTEGER, msg VARCHAR(255))
    BEGIN
      IF enabled THEN
        select concat('** ', msg) AS '** DEBUG:';
      END IF;
    END $$
    
    CREATE PROCEDURE test_procedure(arg1 INTEGER, arg2 INTEGER)
    BEGIN
      SET @enabled = TRUE;
    
      call debug_msg(@enabled, 'my first debug message');
      call debug_msg(@enabled, (select concat_ws('','arg1:', arg1)));
      call debug_msg(TRUE, 'This message always shows up');
      call debug_msg(FALSE, 'This message will never show up');
    END $$
    
    DELIMITER ;
    

    Then run the test like this:

    CALL test_procedure(1,2)
    

    It will result in the following output:

    ** DEBUG:
    ** my first debug message
    ** DEBUG:
    ** arg1:1
    ** DEBUG:
    ** This message always shows up
    

提交回复
热议问题