Debugging procedures and functions using myDebugger for MySQL



This article describes how to debug procedures and functions with a help of Debugger for MySQL.





Since version 5.0, MySQL server supports stored procedures and functions. Procedures and functions allow moving out database’s executable code. It allows reducing resources on communication between the database and application and even speeding up execution.

Sometimes you have to write more or less complicated procedures that in development may require code debugging. Despite the fact that the release version of MySQL 5.5 has been already released, a server-side code debugger still has not been implemented. Therefore, not many programs can boast of the ability to debug procedures and functions. I know two such tools – Debugger for MySQL that will are going to describe and dbForge Studio for MySQL.



Description of debugger and its capabilities.


Let’s review the capabilities of the Debugger for MySQL on an example. Run Debugger for MySQL and enter connection details. That’s it. Now you can start working.


Debugging procedures and functions using myDebugger for MySQL


We have the main menu, toolbar, main window of the debugger – \’Main\’, \’Schema Browser\’ window with the explorer tree of procedures and functions stored on the server, and additional windows: \’Breakpoints\’, \’Callstack\’, \’Watches\’, \’Local variables\’, \’Output\’. All the windows can be moved or docked in the needed position.

Let’s assume we have a table `emp`, from which we want to get some data about an employer. The CREATE TABLE statement for the `emp` table is given in the appendix at the end of the article. Let’s enter the following code in the \’Main\’ window:

To check code for errors, you have to perform the \’Compile (Ctrl+F9)\’ command. If there are no errors, it’s okay. Now let’s set a breakpoint on the second line and perform the \’Run (F9)\’ command after which additional commands become available. You can open the \’Watches\’ window and enter the @s and @d variables for analysis. The \’Step over (F8)\’ command moves the execution cursor to the next action in code and executes it. You can view the value of a variable in the \’Watches\’ window.


Debugging procedures and functions using myDebugger for MySQL



Creation of procedure and saving it on server.


The Debuger for MySQL allows creating new procedures and functions on a server. You just need to select the \’Create procedure…\’ or \’Create function…\’ command in the \’File\’ menu, enter a name, type new code of the procedure/function in the opened window, and save it by performing the \’Store routine (Ctrl+S)\’ command. Let’s try to create the `calc_distance` function based on the formulas in the \’Calculate distance, bearing and more between Latitude/Longitude points\’ article ( to calculate a distance. To do this, select the \’Create function…\’ command, enter the name `calc_distance`, and in the editor window \’Main\’, enter the code:

To save a function on a server, perform the \’Store routine (Ctrl+S)\’ command. That’s it, our function was successfully saved.



Example of debugging procedure with parameters.


Let’s review the variant of stored function debugger with the input parameters (IN). How to set the input parameters with the debugger and to get a resulting value as a result? As an example, let’s use our function ‘calc_distance’, created in the previous section; let’s try to debug it in the Debugger for MySQL. Obviously to do it we need to execute a function and to set input parameters; also we should write code in which we would write sting with function calling, for example \’SELECT function1(10)\’ or \’CALL parocedure1()\’ if we would call a procedure.


Overall, we need to prepare an environment for debugger. And in this case Debugger for MySQL helps us, it’s enough in \’Schema browser\’ to allocate the needed function and to choose the command \’Configure environment for debug this routine\’, herewith all infrastructure will be created automatically.


In the window \’Main\’ we will get code with function calling, where we can set and count the parameter values and the window with function body. Herewith, one breakpoint automatically will be set at the beginning of the function body.

Lets’ input text coordinates for calculation of distance and set breakpoint in the calculation result string:


Debugging procedures and functions using myDebugger for MySQL


Also, we put breakpoint in the needed string of the function window `calc_distance` and run command \’Run (F9)\’, herewith at the beginning an initialization of the function executing (window \’Main\’) will be done and then by call stack we will get into calling function `calc_distance` and will stop on breakpoint, for visualization values of users variables which were set in the function are shown in the window \’Local variables\’: 


Debugging procedures and functions using myDebugger for MySQL


Then it’s up to a programmer, with the command help \’Step into (F7)\’ and \’Step over (F8)\’ we can move forward by code and analyze the way of execution. If our function would call another procedure or function, then in the command execution \’Step into (F7)\’ (for example, on statement CALL) the window with code of the calling procedure/function would open, and we would automatically move to code of its procedure/function.

As a result, after finishing of the function execution `calc_distance`, we get back to the window \’Main\’ where we can check the work results of function, analyzing the received meaning.


Debugging procedures and functions using myDebugger for MySQL



Appendix – code to create table `emp`.


Leave a Reply

Your email address will not be published. Required fields are marked *