{"id":27732,"date":"2022-12-22T12:39:05","date_gmt":"2022-12-22T20:39:05","guid":{"rendered":"https:\/\/coderpad.io\/?p=27732"},"modified":"2023-06-05T13:46:56","modified_gmt":"2023-06-05T20:46:56","slug":"advanced-stored-procedures-in-mysql","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/advanced-stored-procedures-in-mysql\/","title":{"rendered":"Advanced Stored Procedures In MySQL"},"content":{"rendered":"\n<p>In our <a href=\"https:\/\/coderpad.io\/blog\/development\/an-introduction-to-stored-procedures-in-mysql\/\">previous article<\/a>, we learned the basics of stored procedures and how they can help you manage your MySQL database efficiently. In this guide, we\u2019ll dive deeper and discuss more advanced stored procedures concepts and techniques. By the end of this article, you\u2019d have learned some skills that will make you more proficient in working with stored procedures in MySQL.<\/p>\n\n\n\n<p>Stored procedures share many similarities with functions in procedural languages such as JavaScript, Python, etc. They are used to group and execute many tasks in one call. They can be called and executed repeatedly using loops. They can also be nested in one another and executed in parent-stored procedures. You can also nest MySQL functions in stored procedures and more!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nested stored procedures<\/h2>\n\n\n\n<p>Nested stored procedures help to break up large amounts of SQL statements into smaller reusable pieces. Moving reusable logic pieces into smaller composable pieces can make your procedures more readable and easy to debug.<\/p>\n\n\n\n<p>Whenever you write a statement that calls a procedure X in the body of another stored procedure Y, you\u2019re nesting X in Y. The syntax looks like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> parent_procedure\n\n<span class=\"hljs-keyword\">BEGIN<\/span>\n\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">column<\/span> <span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-keyword\">table<\/span>;\n\n\/\/ other statements here\n\n\/\/ nested stored procedure\n\n<span class=\"hljs-keyword\">CALL<\/span> nested_procedure;\u00a0\n\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Let\u2019s demonstrate how nested stored procedures work. For this example, we\u2019d be refactoring the <code>film_in_stock<\/code> stored procedure we created in <a href=\"https:\/\/coderpad.io\/blog\/development\/an-introduction-to-stored-procedures-in-mysql\/\">our previous article<\/a>. I\u2019ll type it in here for reference:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">DELIMITER \/\/\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> film_in_stock(<span class=\"hljs-keyword\">IN<\/span> p_film_id <span class=\"hljs-built_in\">INT<\/span>, <span class=\"hljs-keyword\">IN<\/span> p_store_id <span class=\"hljs-built_in\">INT<\/span>, <span class=\"hljs-keyword\">OUT<\/span> p_film_count <span class=\"hljs-built_in\">INT<\/span>)\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> inventory_id\n    <span class=\"hljs-keyword\">FROM<\/span> inventory\n    <span class=\"hljs-keyword\">WHERE<\/span> film_id = p_film_id\n    <span class=\"hljs-keyword\">AND<\/span> store_id = p_store_id\n    <span class=\"hljs-keyword\">AND<\/span> inventory_in_stock(inventory_id);\n\n    <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*)\n    <span class=\"hljs-keyword\">FROM<\/span> inventory\n    <span class=\"hljs-keyword\">WHERE<\/span> film_id = p_film_id\n    <span class=\"hljs-keyword\">AND<\/span> store_id = p_store_id\n    <span class=\"hljs-keyword\">AND<\/span> inventory_in_stock(inventory_id)\n    <span class=\"hljs-keyword\">INTO<\/span> p_film_count;\n<span class=\"hljs-keyword\">END<\/span> \/\/\n\nDELIMITER;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We can take the second <code>SELECT<\/code> statement and place it into its own stored procedure named <code>count_inventory<\/code> so that we can reuse the logic in other places.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">DELIMITER \/\/\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> count_inventory(<span class=\"hljs-keyword\">IN<\/span> p_film_id <span class=\"hljs-built_in\">INT<\/span>, <span class=\"hljs-keyword\">IN<\/span> p_store_id <span class=\"hljs-built_in\">INT<\/span>, <span class=\"hljs-keyword\">OUT<\/span> p_film_count <span class=\"hljs-built_in\">INT<\/span>)\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*)\n    <span class=\"hljs-keyword\">FROM<\/span> inventory\n    <span class=\"hljs-keyword\">WHERE<\/span> film_id = p_film_id\n    <span class=\"hljs-keyword\">AND<\/span> store_id = p_store_id\n    <span class=\"hljs-keyword\">AND<\/span> inventory_in_stock(inventory_id)\n    <span class=\"hljs-keyword\">INTO<\/span> p_film_count;\n<span class=\"hljs-keyword\">END<\/span> \/\/\n\nDELIMITER;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>count_inventory<\/code> stored procedure will accept three arguments just like it\u2019s parent procedure:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>p_film_id<\/code> defines the variable for the film\u2019s id<\/li>\n\n\n\n<li><code>p_store_id<\/code> defines the variable for the store\u2019s id<\/li>\n\n\n\n<li><code>p_film_count<\/code> will hold the total number of films that were found in the inventory using the filters passed.<\/li>\n<\/ol>\n\n\n\n<p>The parent procedure <code>film_in_stock<\/code> will now look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">DELIMITER \/\/\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> film_in_stock(<span class=\"hljs-keyword\">IN<\/span> p_film_id <span class=\"hljs-built_in\">INT<\/span>, <span class=\"hljs-keyword\">IN<\/span> p_store_id <span class=\"hljs-built_in\">INT<\/span>, <span class=\"hljs-keyword\">OUT<\/span> p_film_count <span class=\"hljs-built_in\">INT<\/span>)\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> inventory_id\n    <span class=\"hljs-keyword\">FROM<\/span> inventory\n    <span class=\"hljs-keyword\">WHERE<\/span> film_id = p_film_id\n    <span class=\"hljs-keyword\">AND<\/span> store_id = p_store_id\n    <span class=\"hljs-keyword\">AND<\/span> inventory_in_stock(inventory_id);\n\n    <span class=\"hljs-keyword\">CALL<\/span> count_inventory(p_film_id, p_store_id, p_film_count);\n<span class=\"hljs-keyword\">END<\/span> \/\/\n\nDELIMITER;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When we call the <code>film_in_stock<\/code> stored procedure, it will make a call to the <code>count_inventory<\/code> stored procedure to count the number of films based on the condition values provided in its <code>IN<\/code> parameters:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">mysql&gt; <span class=\"hljs-keyword\">CALL<\/span> film_in_stock(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>, @p_film_count);\n+<span class=\"hljs-comment\">--------------+<\/span>\n| inventory_id |\n+<span class=\"hljs-comment\">--------------+<\/span>\n|            1 |\n|            2 |\n|            3 |\n|            4 |\n+<span class=\"hljs-comment\">--------------+<\/span>\n4 rows in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.01<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The nested procedure <code>count_inventory<\/code> also let\u2019s us retrieve the inventory count using the user-defined variable we pass into the <code>OUT<\/code> parameter <code>@p_film_count<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">mysql&gt; <span class=\"hljs-keyword\">SELECT<\/span> @p_film_count;\n+<span class=\"hljs-comment\">---------------+<\/span>\n| @p_film_count |\n+<span class=\"hljs-comment\">---------------+<\/span>\n|             4 |\n+<span class=\"hljs-comment\">---------------+<\/span>\n1 row in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>From our example above, you would notice we called a function named <code>inventory_in_stock<\/code>. We won\u2019t go deep into MySQL functions just yet, but the body of the function looks like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> inventory_in_stock (p_inventory_id <span class=\"hljs-built_in\">INT<\/span>) <span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-built_in\">tinyint<\/span>(<span class=\"hljs-number\">1<\/span>)\n    <span class=\"hljs-keyword\">READS<\/span> <span class=\"hljs-keyword\">SQL<\/span> <span class=\"hljs-keyword\">DATA<\/span>\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> v_rentals <span class=\"hljs-built_in\">INT<\/span>;\n    <span class=\"hljs-keyword\">DECLARE<\/span> v_out     <span class=\"hljs-built_in\">INT<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">INTO<\/span> v_rentals\n    <span class=\"hljs-keyword\">FROM<\/span> rental\n    <span class=\"hljs-keyword\">WHERE<\/span> inventory_id = p_inventory_id;\n\n    IF v_rentals = 0 THEN\n      RETURN TRUE;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(rental_id) <span class=\"hljs-keyword\">INTO<\/span> v_out\n    <span class=\"hljs-keyword\">FROM<\/span> inventory <span class=\"hljs-keyword\">LEFT<\/span> <span class=\"hljs-keyword\">JOIN<\/span> rental <span class=\"hljs-keyword\">USING<\/span>(inventory_id)\n    <span class=\"hljs-keyword\">WHERE<\/span> inventory.inventory_id = p_inventory_id\n    <span class=\"hljs-keyword\">AND<\/span> rental.return_date <span class=\"hljs-keyword\">IS<\/span> <span class=\"hljs-literal\">NULL<\/span>;\n\n    IF v_out &gt; 0 THEN\n      RETURN FALSE;\n    ELSE\n      RETURN TRUE;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The point I\u2019m trying to make is that you can also nest MySQL functions into stored procedures.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Variables in stored procedures<\/h2>\n\n\n\n<p>Variables are very useful when writing stored procedures. They hold immediate results and can be referenced anywhere in the body of a stored procedure. The value of a variable can range from primitive data types and objects to results from calculations, or data returned from functions and nested stored procedures. This value can change during the execution of the stored procedure.&nbsp;<\/p>\n\n\n\n<p>A variable is always assigned a name and you can reference that name to retrieve the current value the variable holds.<\/p>\n\n\n\n<p>There are three types of variables in MySQL, all of which can be used in a stored procedure based on scope. These are local variables, user-defined variables and system variables. Scope in MySQL actually refers to the current context of code that determines how variables can be accessed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Declaring local variables in stored procedures<\/h3>\n\n\n\n<p>Local variables are usually scoped within the <code>BEGIN<\/code> and <code>END<\/code> clause of a stored procedure.&nbsp;<\/p>\n\n\n\n<p>To make use of a local variable in a stored procedure, you have to first declare it using the <code>DECLARE<\/code> statement in the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> variable_name data_type(<span class=\"hljs-keyword\">size<\/span>) &#91;<span class=\"hljs-keyword\">DEFAULT<\/span> default_value];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The variable name follows the <code>DECLARE<\/code> statement and must obey the rules of naming columns in MySQL. Next, we specify the datatype and length of the variable. A variable\u2019s value is NULL by default, but we can specify another value using the <code>DEFAULT<\/code> clause.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Assigning variables to new values<\/h4>\n\n\n\n<p>You can change the existing value of a variable by re-assigning it to a new value. This is done using the <code>SET<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SET<\/span> variable_name = <span class=\"hljs-keyword\">value<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> total <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">0<\/span>;\n<span class=\"hljs-keyword\">SET<\/span> total = <span class=\"hljs-number\">1800<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can also pass the result of a query into a variable using the <code>SELECT INTO<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> inventory_count <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">0<\/span>;\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">INTO<\/span> inventory_count <span class=\"hljs-keyword\">FROM<\/span> inventory;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following stored procedure uses local variables to fetch all users that have surnames that begin with the letter A through D:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">DELIMITER \/\/\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> fetch_users()\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">start<\/span> <span class=\"hljs-built_in\">CHAR<\/span>(<span class=\"hljs-number\">1<\/span>) <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-string\">'A'<\/span>;\n    <span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">end<\/span> <span class=\"hljs-built_in\">CHAR<\/span>(<span class=\"hljs-number\">1<\/span>) <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-string\">'D'<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span> lastname <span class=\"hljs-keyword\">FROM<\/span> actor <span class=\"hljs-keyword\">WHERE<\/span> lastname <span class=\"hljs-keyword\">BETWEEN<\/span> <span class=\"hljs-keyword\">start<\/span> <span class=\"hljs-keyword\">AND<\/span> <span class=\"hljs-keyword\">end<\/span>;\n<span class=\"hljs-keyword\">END<\/span> \/\/\n\nDELIMITER;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">User-defined variables<\/h3>\n\n\n\n<p>In our past example, we created a user-defined variable <code>@p_film_count<\/code> to hold the value of the inventory count of the films. User-defined variables are session specific, meaning the user variables defined in a client cannot be seen by other clients except the user can access the Performance Schema <code>user_variables_by_thread<\/code> table.<\/p>\n\n\n\n<p>User-defined variables are always written by prefixing the letter <code>@<\/code> with the name of the variable, e.g. <code>@total_orders<\/code>. The name of a user-defined variable consists of alphanumeric characters, <code>.<\/code>, <code>_<\/code>, and <code>$<\/code>.&nbsp; To use other characters, you must quote it as a string or identifier, e.g. <code>@'my_var'<\/code> or <code>@\"my_var\"<\/code>.<\/p>\n\n\n\n<p>Unlike local variables, you can access a user-defined variable without declaring it. If you use a user-defined variable that has not been declared, its value will be <code>NULL<\/code> by default and it will be a type of string. User variables can be assigned a value from a limited set of data types: integer, decimal, floating-point, binary or nonbinary string, or <code>NULL<\/code> value.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> @var_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can initialize a user-defined variable using the <code>SET<\/code> or <code>SELECT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SET<\/span> @<span class=\"hljs-keyword\">start<\/span> = <span class=\"hljs-string\">'A'<\/span>, @<span class=\"hljs-keyword\">end<\/span> = <span class=\"hljs-string\">'D'<\/span>;\n<span class=\"hljs-keyword\">SELECT<\/span> lastname <span class=\"hljs-keyword\">FROM<\/span> actor <span class=\"hljs-keyword\">WHERE<\/span> lastname <span class=\"hljs-keyword\">BETWEEN<\/span> <span class=\"hljs-keyword\">start<\/span> <span class=\"hljs-keyword\">AND<\/span> <span class=\"hljs-keyword\">end<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> @<span class=\"hljs-keyword\">start<\/span> := <span class=\"hljs-string\">'A'<\/span>, @<span class=\"hljs-keyword\">end<\/span> := <span class=\"hljs-string\">'D'<\/span>;\n<span class=\"hljs-keyword\">SELECT<\/span> lastname <span class=\"hljs-keyword\">FROM<\/span> actor <span class=\"hljs-keyword\">WHERE<\/span> lastname <span class=\"hljs-keyword\">BETWEEN<\/span> <span class=\"hljs-keyword\">start<\/span> <span class=\"hljs-keyword\">AND<\/span> <span class=\"hljs-keyword\">end<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">System variables<\/h3>\n\n\n\n<p>System variables are variables that are readily available for use in the MySQL server. These variables are maintained by the MySQL server and they are used to configure its operation. The system variables are usually classified into <code>GLOBAL<\/code>, <code>SESSION<\/code> or both.<\/p>\n\n\n\n<p>&nbsp;All system variables have default values and they can be set at server startup using the command line or with an option file. Some can even be changed at runtime using the <code>SET<\/code> statement. You can use system variables in expressions and even in the body of a stored procedure.<\/p>\n\n\n\n<p>You can see the current value of a system variable used by the MySQL server with the <code>SHOW VARIABLES<\/code> statement or the <code>SELECT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">SHOW<\/span> &#91;<span class=\"hljs-keyword\">GLOBAL<\/span> | <span class=\"hljs-keyword\">SESSION<\/span>] <span class=\"hljs-keyword\">VARIABLES<\/span> &#91;<span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'pattern'<\/span> | <span class=\"hljs-keyword\">WHERE<\/span> expr]\n<span class=\"hljs-keyword\">SELECT<\/span> @@var_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When using the SHOW VARIABLES statement without the <code>GLOBAL<\/code> or <code>SESSION<\/code> modifier the default modifier is <code>SESSION<\/code>. When using the SELECT statement, you must prefix the system variable name with <code>@@<\/code> so that the MySQL server knows you\u2019re referencing a system variable and not a user-defined or local variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conditionals (<code>IF<\/code> statement)<\/h2>\n\n\n\n<p>The MySQL <code>IF<\/code> statement allows you to execute one or more SQL statements based on a condition. The <code>IF<\/code> statement can have <code>THEN<\/code>, <code>ELSE<\/code>, and <code>ELSEIF<\/code> clauses, and is terminated using <code>END IF;<\/code>.&nbsp;<\/p>\n\n\n\n<p>The basic syntax of the <code>IF<\/code> statement is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">IF condition THEN statement_list\n    &#91;ELSEIF condition THEN statement_list] ...\n    &#91;ELSE statement_list]\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the <code>condition<\/code> evaluates to true , the <code>statement_list<\/code> (one or more SQL statements) will execute. But if no <code>condition<\/code> matches, the <code>ELSE<\/code> clause statement_list will execute.<\/p>\n\n\n\n<p>The following stored procedure <code>greeting<\/code> checks returns a greeting based on the current time of day:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">DELIMITER \/\/\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> greeting()\n<span class=\"hljs-keyword\">BEGIN<\/span>\n<span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">hour<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">HOUR<\/span>(<span class=\"hljs-keyword\">NOW<\/span>());\n<span class=\"hljs-keyword\">DECLARE<\/span> message <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">20<\/span>);\n\nIF hour &lt; 18 THEN\n<span class=\"hljs-keyword\">SET<\/span> message = <span class=\"hljs-string\">\"Good day!\"<\/span>;\nELSE\n<span class=\"hljs-keyword\">SET<\/span> message =  <span class=\"hljs-string\">\"Good evening!\"<\/span>;\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n\n<span class=\"hljs-keyword\">SELECT<\/span> message;\n<span class=\"hljs-keyword\">END<\/span> \/\/\n\nDELIMITER ;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Loops in stored procedures<\/h2>\n\n\n\n<p>Loops can be used in repeating the execution of one or more SQL statements until a condition is met. To terminate a loop if a condition has been met you can use the <code>LEAVE<\/code> statement. The basic syntax of a loop is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">&#91;begin_label:] LOOP\n    statement_list\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">LOOP<\/span> &#91;end_label]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>LOOP<\/code> statements just like <code>REPEAT<\/code> and <code>WHILE<\/code> statements are permitted to use labels if need be under the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>begin_label<\/code> must be followed by a colon<\/li>\n\n\n\n<li><code>begin_label<\/code> can be given without <code>end_label<\/code>. If <code>end_label<\/code> is present, it must be the same as <code>begin_label<\/code>.<\/li>\n\n\n\n<li><code>end_label<\/code> cannot be given without <code>begin_label<\/code>.<\/li>\n\n\n\n<li>Labels at the same nesting level must be distinct.<\/li>\n\n\n\n<li>Labels can be up to 16 characters long.<\/li>\n<\/ul>\n\n\n\n<p>The general syntax when using a LEAVE statement in a LOOP looks like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">&#91;label1:] LOOP\n    ...\n    <span class=\"hljs-comment\">\/* Terminate the loop *\/<\/span>\n    IF condition THEN\n        LEAVE &#91;label1];\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n    ...\n<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">LOOP<\/span> &#91;label1]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>While the LEAVE statement is used to quit or terminate the loop, the opposite <code>ITERATE<\/code> is used to start the loop again. The syntax looks like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">label1: LOOP    ...    <span class=\"hljs-comment\">\/* Start the loop again *\/<\/span>\n    IF condition THEN ITERATE label1; END IF;\n    ...\nEND LOOP label1;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses both statements to continue iterating or terminate the loop based on a condition:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> doiterate(p1 <span class=\"hljs-built_in\">INT<\/span>)\n<span class=\"hljs-keyword\">BEGIN<\/span>\n  label1: <span class=\"hljs-keyword\">LOOP<\/span>\n    <span class=\"hljs-keyword\">SET<\/span> p1 = p1 + <span class=\"hljs-number\">1<\/span>;\n    IF p1 &lt; 10 THEN ITERATE label1; <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n    LEAVE label1;\n  <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">LOOP<\/span> label1;\n<span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Cursors<\/h2>\n\n\n\n<p>When working with large rows of data, you may want to process the data on a row-by-row basis. That\u2019s where cursors come in. Cursors are used to iterate through the results of a query allowing you to perform certain operations on each row of data individually.<\/p>\n\n\n\n<p>In MySQL, Cursors are supported in stored programs such as stored procedures, stored functions, and triggers, and they have the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Asensitive: The server may or may not make a copy of its result table<\/li>\n\n\n\n<li>Read only: Cursors cannot be used to modify the data of the resulting table.<\/li>\n\n\n\n<li>Nonscrollable: Can be traversed only in one direction and cannot skip rows<\/li>\n<\/ul>\n\n\n\n<p>Cursors are to be declared after you must have declared variables and conditions and also before you declare any handlers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> curdemo()\n<span class=\"hljs-keyword\">BEGIN<\/span>\n  <span class=\"hljs-keyword\">DECLARE<\/span> done <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-literal\">FALSE<\/span>;\n  <span class=\"hljs-keyword\">DECLARE<\/span> a <span class=\"hljs-built_in\">CHAR<\/span>(<span class=\"hljs-number\">16<\/span>);\n  <span class=\"hljs-keyword\">DECLARE<\/span> b, c <span class=\"hljs-built_in\">INT<\/span>;\n\n  <span class=\"hljs-comment\">\/* Declare cursors: cur1 and cur2 *\/<\/span>\n  <span class=\"hljs-keyword\">DECLARE<\/span> cur1 <span class=\"hljs-keyword\">CURSOR<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">id<\/span>,<span class=\"hljs-keyword\">data<\/span> <span class=\"hljs-keyword\">FROM<\/span> test.t1;\n  <span class=\"hljs-keyword\">DECLARE<\/span> cur2 <span class=\"hljs-keyword\">CURSOR<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">SELECT<\/span> i <span class=\"hljs-keyword\">FROM<\/span> test.t2;  <span class=\"hljs-comment\">\/* Declare handler to handle the condition when cursor reaches end of result set *\/<\/span>\n  <span class=\"hljs-keyword\">DECLARE<\/span> CONTINUE <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">FOUND<\/span> <span class=\"hljs-keyword\">SET<\/span> done = <span class=\"hljs-literal\">TRUE<\/span>;\n\n  <span class=\"hljs-comment\">\/* Open the cursors to initialize result set*\/<\/span>\n  OPEN cur1;\n  OPEN cur2;\n\n  read_loop: LOOP\n\n    <span class=\"hljs-comment\">\/* FETCH retrieves the rows the cursors currently point to, then updates cursors to the next row *\/<\/span>\n    FETCH cur1 INTO a, b;\n    FETCH cur2 INTO c;\n    IF done THEN\n      LEAVE read_loop;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n    IF b &lt; c THEN\n      <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> test.t3 <span class=\"hljs-keyword\">VALUES<\/span> (a,b);\n    ELSE\n      <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> test.t3 <span class=\"hljs-keyword\">VALUES<\/span> (a,c);\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n  <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">LOOP<\/span>;\n\n  <span class=\"hljs-comment\">\/* Close cursors when no longer used to release memory used *\/<\/span>\n  CLOSE cur1;\n  CLOSE cur2;\n<span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The example above shows how cursors work in the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, we declare the cursors <code>cur1<\/code> and <code>cur2<\/code> using the <code>DECLARE...CURSOR FOR<\/code> statement. Notice how it appears after the variable declarations and before the handlers.<\/li>\n\n\n\n<li>Next, we open the cursors, basically, initializing them for use using the <code>OPEN<\/code> statement.<\/li>\n\n\n\n<li>Then we use the <code>FETCH<\/code> statement to get the next rows of data a cursor currently points to, the <code>FETCH<\/code> statement also updates the cursor to point to the following row.<\/li>\n\n\n\n<li>Lastly, when done with the cursors, we use the <code>CLOSE<\/code> statement to deactivate them and release the memory they use.<\/li>\n<\/ul>\n\n\n\n<p>Notice that we also declared a handler to handle the condition when a cursor reaches the end of the result set using the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> CONTINUE <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">FOUND<\/span> <span class=\"hljs-keyword\">SET<\/span> done = <span class=\"hljs-literal\">TRUE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Error handling in stored procedures<\/h2>\n\n\n\n<p>MySQL doesn\u2019t make use of traditional <code>TRY\u2026CATCH<\/code> statements for error handling in stored procedures, instead it uses Condition Handling to handle errors encountered.<\/p>\n\n\n\n<p>The <code>DECLARE...HANDLER<\/code> statement is used to specify a handler that deals with one or more conditions. It has the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> handler_action <span class=\"hljs-keyword\">HANDLER<\/span>\n    <span class=\"hljs-keyword\">FOR<\/span> condition_value &#91;, condition_value] ...\n    <span class=\"hljs-keyword\">statement<\/span>\n\n\n\nhandler_action: {\n    CONTINUE\n  | <span class=\"hljs-keyword\">EXIT<\/span>\n}\n\ncondition_value: {\n    mysql_error_code\n  | <span class=\"hljs-keyword\">SQLSTATE<\/span> &#91;<span class=\"hljs-keyword\">VALUE<\/span>] sqlstate_value\n  | condition_name\n  | SQLWARNING\n  | <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">FOUND<\/span>\n  | SQLEXCEPTION\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>From the syntax above, if one of the conditions (<code>condition_value<\/code>) are met, <code>statement<\/code> will execute. <code>statement<\/code> can be a simple statement such as <code>SET var_name = value<\/code>, or a compound statement written using <code>BEGIN<\/code> and <code>END<\/code>.<\/p>\n\n\n\n<p>The <code>handler_action<\/code> represents the action the handler takes after the execution of the handler statement. It can be either of the following values:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>CONTINUE<\/code>: Execution of the current program continues.<\/li>\n\n\n\n<li><code>EXIT<\/code>: Execution terminates for the BEGIN &#8230; END compound statement in which the handler is declared. This is true even if the condition occurs in an inner block.<\/li>\n<\/ul>\n\n\n\n<p>The <code>condition_value<\/code> indicates the specific condition or class of conditions that activates the handler. It can be one of the following types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mysql_error_code<\/code>: This is an integer literal that indicates a MySQL error code. For example <code>1051<\/code> which means <code>Unknown table '%s'<\/code>. For example:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> CONTINUE <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-number\">1051<\/span>\n  <span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-comment\">-- body of handler<\/span>\n  <span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>SQLSTATE [VALUE] <em>sqlstate_value<\/em><\/code>: This is a 5-character string literal that indicates an SQLSTATE value, such as \u2018<strong>42S01<\/strong>\u2019 to specify \u201c<strong>unknown table<\/strong>\u201d. For example:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> CONTINUE <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">SQLSTATE<\/span> <span class=\"hljs-string\">'42S02'<\/span>\n  <span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-comment\">-- body of handler<\/span>\n  <span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>SQLWARNING<\/code>: This is the shorthand for the class of SQLSTATE values that begin with <code>'01'<\/code>:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> CONTINUE <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> SQLWARNING\n  <span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-comment\">-- body of handler<\/span>\n  <span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>NOT FOUND<\/code>:&nbsp; This is the shorthand for the class of SQLSTATE values that begin with <code>'02'<\/code>. This is usually used together with cursors to control what happens when a cursor reaches the end of a data set. When no more rows are available, MySQL raises a No Data condition with a SQLSTATE value <code>'02000'<\/code>. Setting up a handler for a <code>NOT FOUND<\/code> condition allows you to detect the condition. We illustrated how to set up such a handler when we treated cursors earlier.<\/li>\n\n\n\n<li><code>SQLEXCEPTION<\/code>: This is the shorthand for the class of SQLSTATE values that begin with <code>'00'<\/code>, <code>'01'<\/code>, or <code>'02'<\/code>.<\/li>\n\n\n\n<li><code>condition_name<\/code>: You can define your own condition name that is associated with a MySQL error code or <code>SQLSTATE<\/code> value. To declare a named error condition, you make use of the <code>DECLARE\u2026CONDITION<\/code> statement as follows:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">DECLARE<\/span> condition_name CONDITION <span class=\"hljs-keyword\">FOR<\/span> condition_value<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here <code>condition_name<\/code> represents the name for the condition and <code>condition_value<\/code> represents the MySQL error code or <code>SQLSTATE<\/code> value the condition is associated with.<\/p>\n\n\n\n<p>The following example uses a handler for <code>SQLSTATE \u201823000\u2019<\/code>, which occurs for a duplicate-key error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\">mysql&gt; <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> test.t (s1 <span class=\"hljs-built_in\">INT<\/span>, PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (s1));\nQuery OK, 0 rows affected (0.00 sec)\n\nmysql&gt; delimiter \/\/\n\nmysql&gt; <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> handlerdemo ()\n      <span class=\"hljs-keyword\">BEGIN<\/span>\n        <span class=\"hljs-keyword\">DECLARE<\/span> CONTINUE <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">SQLSTATE<\/span> <span class=\"hljs-string\">'23000'<\/span> <span class=\"hljs-keyword\">SET<\/span> @x2 = <span class=\"hljs-number\">1<\/span>;\n        <span class=\"hljs-keyword\">SET<\/span> @x = <span class=\"hljs-number\">1<\/span>;\n        <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> test.t <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-number\">1<\/span>);\n        <span class=\"hljs-keyword\">SET<\/span> @x = <span class=\"hljs-number\">2<\/span>;\n        <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> test.t <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-number\">1<\/span>);\n        <span class=\"hljs-keyword\">SET<\/span> @x = <span class=\"hljs-number\">3<\/span>;\n      <span class=\"hljs-keyword\">END<\/span>;\n      \/\/\nQuery OK, 0 rows affected (0.00 sec)\nmysql&gt; delimiter ;\n\nmysql&gt; <span class=\"hljs-keyword\">CALL<\/span> handlerdemo();\nQuery OK, 0 rows affected (0.00 sec)\n\nmysql&gt; <span class=\"hljs-keyword\">SELECT<\/span> @x;\n+<span class=\"hljs-comment\">------+<\/span>\n| @x   |\n+<span class=\"hljs-comment\">------+<\/span>\n| 3    |\n+<span class=\"hljs-comment\">------+<\/span>\n1 row in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Scope rules for handlers<\/h3>\n\n\n\n<p>MySQL handlers are invoked based on their location within the stored program\u2019s definition and on the conditions they handle:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A handler declared between a <code>BEGIN...END<\/code> block is in scope only for the SQL statements following the handler\u2019s declaration within the block. In the following example, handlers <code>H1<\/code> and <code>H2<\/code> are in scope for conditions raised by statements <code>stmt1<\/code> and <code>stmt2<\/code>. But neither <code>H1<\/code> nor <code>H2<\/code> are in scope for conditions raised in the body of <code>H1<\/code> or <code>H2<\/code>:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">BEGIN<\/span> <span class=\"hljs-comment\">-- outer block<\/span>\n  <span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">EXIT<\/span> <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> ...;  <span class=\"hljs-comment\">-- handler H1<\/span>\n  <span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">EXIT<\/span> <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> ...;  <span class=\"hljs-comment\">-- handler H2<\/span>\n  stmt1;\n  stmt2;\n<span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>A handler is in scope only for the block in which it is declared. It cannot be activated for conditions occurring outside that block. In the following example, handler <code>H1<\/code> is in scope for <code>stmt1<\/code> in the inner block, but not for <code>stmt2<\/code> in the outer block:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql shcb-wrap-lines\"><span class=\"hljs-keyword\">BEGIN<\/span> <span class=\"hljs-comment\">-- outer block<\/span>\n  <span class=\"hljs-keyword\">BEGIN<\/span> <span class=\"hljs-comment\">-- inner block<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">EXIT<\/span> <span class=\"hljs-keyword\">HANDLER<\/span> <span class=\"hljs-keyword\">FOR<\/span> ...;  <span class=\"hljs-comment\">-- handler H1<\/span>\n    stmt1;\n  <span class=\"hljs-keyword\">END<\/span>;\n  stmt2;\n<span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>A handler can be specific or general. Specific handlers are for MySQL error codes, <code>SQLSTATE<\/code> values, or condition names whereas general handlers are for conditions in the <code>SQLWARNING<\/code>, <code>SQLEXCEPTION<\/code>, or <code>NOT FOUND<\/code> class.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Condition specificity is related to condition precedence.&nbsp;<\/h4>\n\n\n\n<p>When a condition occurs in a stored program, MySQL server searches for applicable handlers in the current scope. If there are no applicable handlers, it checks outside the scope and continues outwards for any applicable handlers. When the server finds one or more applicable handlers at a given scope, it chooses among them based on condition precedence:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A MySQL error code handler takes precedence over an <code>SQLSTATE<\/code> value handler.<\/li>\n\n\n\n<li>An <code>SQLSTATE<\/code> value handler takes precedence over general <code>SQLWARNING<\/code>, <code>SQLEXCEPTION<\/code>, or <code>NOT FOUND<\/code> handlers.<\/li>\n\n\n\n<li>An <code>SQLEXCEPTION<\/code> handler takes precedence over an <code>SQLWARNING<\/code> handler.<\/li>\n\n\n\n<li>For the case where several applicable handlers have the same precedence, the choice of which handler the server activates is nondeterministic and may change depending on the circumstances under which the condition occurs.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial you have learned the more advanced concepts for working with stored procedures. You learned how to improve your stored procedures definitions using variables, nested procedures, loops, and conditionals. We also covered cursors and condition handling in stored procedures. Stored procedures are not the only way to bundle and execute one or more SQL statements in MySQL. In our next MySQL tutorial, we will learn how stored functions can be used in performing operations on the database, we will also explore the differences with Stored procedures. Until then, happy coding!<\/p>\n\n\n\n<p>Also, feel free to explore the CoderPad blog for more database-oriented posts, such as these:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\">SQL Functions and Techniques Every Data Person Should Know<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/optimize-mysql-database-schema\/\">Optimize MySQL Database Schema<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/an-introduction-to-stored-procedures-in-mysql\/\">An Introduction To Stored Procedures In MySQL<\/a><\/li>\n<\/ul>\n\n\n\n<p><em>I\u2019m Elvis Duru. I create helpful content for web developers with a focus on React, Node.js, SQL\/NoSQL Databases, and more! Let\u2019s connect on<\/em><a href=\"https:\/\/twitter.com\/ElvisDuru\" target=\"_blank\" rel=\"noopener\"><em> Twitter<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many database management systems today can use stored procedures to execute tasks on the database. Learn the concepts and techniques of advanced stored procedures in MySQL from this blogpost.<\/p>\n","protected":false},"author":1,"featured_media":27834,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[66],"keyword-cluster":[],"class_list":["post-27732","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/27732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=27732"}],"version-history":[{"count":124,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/27732\/revisions"}],"predecessor-version":[{"id":27857,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/27732\/revisions\/27857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/27834"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=27732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=27732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=27732"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=27732"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=27732"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=27732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}