How do you debug MySQL stored procedures?

后端 未结 16 1281
旧时难觅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

    MySQL user defined variable (shared in session) could be used as logging output:

    DELIMITER ;;
    CREATE PROCEDURE Foo(tableName VARCHAR(128))
    BEGIN
      SET @stmt = CONCAT('SELECT * FROM ', tableName);
      PREPARE pStmt FROM @stmt;
      EXECUTE pStmt;
      DEALLOCATE PREPARE pStmt;
      -- uncomment after debugging to cleanup
      -- SET @stmt = null;
    END;;
    DELIMITER ;
    
    call Foo('foo');
    select @stmt;
    

    will output:

    SELECT * FROM foo
    

提交回复
热议问题