{"id":535,"date":"2009-12-27T13:26:43","date_gmt":"2009-12-27T13:26:43","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=535"},"modified":"2024-02-06T19:14:11","modified_gmt":"2024-02-07T02:14:11","slug":"sql-cursor-in-stored-procedures","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/","title":{"rendered":"MySQL Cursor"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use <strong>MySQL cursor<\/strong> in stored procedures to iterate through a result set returned by a <code>SELECT<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL cursor<\/h2>\n\n\n\n<p>In MySQL, a cursor is a database object used for iterating the result of a <code>SELECT<\/code> statement.<\/p>\n\n\n\n<p>Typically, you use cursors within stored procedures, triggers, and functions where you need <strong>to process individual rows returned by a query one at a time<\/strong>.<\/p>\n\n\n\n<p>Here&#8217;s the basic syntax of a cursor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">-- <span class=\"hljs-keyword\">declare<\/span> a cursor\n<span class=\"hljs-keyword\">DECLARE<\/span> cursor_name CURSOR <span class=\"hljs-keyword\">FOR<\/span> \nSELECT column1, column2 \nFROM your_table \nWHERE your_condition;\n\n-- open the cursor\nOPEN cursor_name;\n\nFETCH cursor_name INTO variable1, variable2;\n-- process the data\n\n\n-- close the cursor\nCLOSE cursor_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works:<\/p>\n\n\n\n<p>First, declare a cursor by using the <code>DECLARE<\/code> statement:<\/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\"><span class=\"hljs-keyword\">DECLARE<\/span> cursor_name <span class=\"hljs-keyword\">CURSOR<\/span> <span class=\"hljs-keyword\">FOR<\/span> \n<span class=\"hljs-keyword\">SELECT<\/span> column1, column2 \n<span class=\"hljs-keyword\">FROM<\/span> your_table \n<span class=\"hljs-keyword\">WHERE<\/span> your_condition;<\/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>The cursor declaration must come after any <a title=\"MySQL Variables in Stored Procedures\" href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/variables-in-stored-procedures\/\">variable <\/a>declaration. If you declare a cursor before the variable declarations, MySQL will issue an error. Additionally, a cursor must always associate with a <code>SELECT<\/code> statement.<\/p>\n\n\n\n<p>Next, open the cursor using the <code>OPEN<\/code> statement:<\/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\">OPEN cursor_name;<\/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>OPEN<\/code> statement initializes the result set for the cursor; therefore, you must call the <code>OPEN<\/code> statement before fetching rows from the result set.<\/p>\n\n\n\n<p>Then, retrieve the next row pointed by the cursor and advance the cursor to the subsequent row in the result set using the <code>FETCH<\/code> statement:<\/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\">FETCH cursor_name INTO variable1, variable2;<\/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>After that, check if there is any row available before fetching it.<\/p>\n\n\n\n<p>Finally, deactivate the cursor and release the memory associated with it using the <code>CLOSE<\/code> statement:<\/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\">CLOSE cursor_name;<\/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>It is a good practice to always close a cursor when it is no longer used.<\/p>\n\n\n\n<p>When working with MySQL cursor, you must also declare a <code>NOT FOUND<\/code> handler to manage the situation when the cursor cannot find any row.<\/p>\n\n\n\n<p>Each time you call the <code>FETCH<\/code> statement, the cursor attempts to read the next row in the result set. When the cursor reaches the end of the result set, it will not be able to retrieve the data, and a condition is raised. The handler is used to handle this condition.<\/p>\n\n\n\n<p>To declare a <code>NOT FOUND<\/code> handler, you use the following syntax:<\/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\"><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> finished = <span class=\"hljs-number\">1<\/span>;<\/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>The&nbsp;<code>finished<\/code> is a variable to indicate that the cursor has reached the end of the result set. <\/p>\n\n\n\n<p>Notice that the handler declaration must appear after the variable and cursor declarations within the stored procedures.<\/p>\n\n\n\n<p>The following diagram illustrates how MySQL cursor works.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"88\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png\" alt=\"MySQL Cursor Steps\" class=\"wp-image-1936\" title=\"MySQL Cursor Steps\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png 612w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor-300x43.png 300w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL Cursor Example<\/h2>\n\n\n\n<p>We&#8217;ll develop a stored procedure that creates an email list of all employees in the <code>employees<\/code> table in the&nbsp;<a title=\"MySQL Sample Database\" href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/employees.svg\" alt=\"\" class=\"wp-image-10759\"\/><\/figure>\n\n\n\n<p>The following example illustrates how to use a cursor to iterate all rows in the <code>employees<\/code> table and concatenate the email into a string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">DELIMITER $$\n\nCREATE PROCEDURE create_email_list (\n\tINOUT email_list TEXT\n)\nBEGIN\n\t<span class=\"hljs-keyword\">DECLARE<\/span> done BOOL <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">false<\/span>;\n\t<span class=\"hljs-keyword\">DECLARE<\/span> email_address VARCHAR(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-string\">\"\"<\/span>;\n    \n\t-- <span class=\"hljs-keyword\">declare<\/span> cursor <span class=\"hljs-keyword\">for<\/span> employee email\n\t<span class=\"hljs-keyword\">DECLARE<\/span> cur CURSOR <span class=\"hljs-keyword\">FOR<\/span> SELECT email FROM employees;\n\n\t-- <span class=\"hljs-keyword\">declare<\/span> NOT FOUND handler\n\t<span class=\"hljs-keyword\">DECLARE<\/span> <span class=\"hljs-keyword\">CONTINUE<\/span> HANDLER \n        <span class=\"hljs-keyword\">FOR<\/span> NOT FOUND SET done = <span class=\"hljs-keyword\">true<\/span>;\n\t\n    -- open the cursor\n\tOPEN cur;\n\t\n    SET email_list = <span class=\"hljs-string\">''<\/span>;\n\t\n    process_email: LOOP\n\t\t\n        FETCH cur INTO email_address;\n        \n\t\t<span class=\"hljs-keyword\">IF<\/span> done = <span class=\"hljs-keyword\">true<\/span> THEN \n\t\t\tLEAVE process_email;\n\t\tEND <span class=\"hljs-keyword\">IF<\/span>;\n\t\t\n        -- concatenate the email into the emailList\n\t\tSET email_list = CONCAT(email_address,<span class=\"hljs-string\">\";\"<\/span>,email_list);\n\tEND LOOP;\n    \n    -- close the cursor\n\tCLOSE cur;\n\nEND$$\n\nDELIMITER ;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare the variable <code>done<\/code> and initialize its value to <code>false<\/code> and the <code>email_address<\/code> and initialize its value to <code>''<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">DECLARE<\/span> done BOOL <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">false<\/span>;\n<span class=\"hljs-keyword\">DECLARE<\/span> email_address VARCHAR(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-string\">\"\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, declare a cursor that will fetch <code>email<\/code> from the <code>employees<\/code> table:<\/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\"><span class=\"hljs-keyword\">DEClARE<\/span> cur <span class=\"hljs-keyword\">CURSOR<\/span> <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">SELECT<\/span> email <span class=\"hljs-keyword\">FROM<\/span> employees;<\/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>Third, open the cursor by using the <code>OPEN<\/code> statement:<\/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\">OPEN cur;<\/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>Fourth, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-string-functions\/mysql-concat\/\">concatenate<\/a> each email into the mail_list variable using a loop statement, separating email addresses by semicolons:<\/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\">process_email: LOOP\n\t\t\n        FETCH cur INTO email_address;\n        \n\t\tIF done = true THEN \n\t\t\tLEAVE process_email;\n\t\t<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n\t\t\n        <span class=\"hljs-comment\">-- concatenate the email into the emailList<\/span>\n\t\t<span class=\"hljs-keyword\">SET<\/span> email_list = <span class=\"hljs-keyword\">CONCAT<\/span>(email_address,<span class=\"hljs-string\">\";\"<\/span>,email_list);\n\t<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">LOOP<\/span>;<\/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>Finally, close the cursor using the <code>CLOSE<\/code> statement:<\/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\">CLOSE email_cursor;<\/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<p>The following tests the <code>createEmailList<\/code> stored procedure:<\/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\"><span class=\"hljs-keyword\">CALL<\/span> create_email_list(@email_list); \n<span class=\"hljs-keyword\">SELECT<\/span> @email_list\\G<\/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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">*************************** <span class=\"hljs-number\">1.<\/span> row ***************************\n@email_list: mgerard@classicmodelcars.com;ykato@classicmodelcars.com;mnishi@classicmodelcars.com;tking@classicmodelcars.com;pmarsh@classicmodelcars.com;afixter@classicmodelcars.com;bjones@classicmodelcars.com;lbott@classicmodelcars.com;pcastillo@classicmodelcars.com;ghernande@classicmodelcars.com;lbondur@classicmodelcars.com;gvanauf@classicmodelcars.com;ftseng@classicmodelcars.com;spatterson@classicmodelcars.com;jfirrelli@classicmodelcars.com;lthompson@classicmodelcars.com;ljennings@classicmodelcars.com;abow@classicmodelcars.com;gbondur@classicmodelcars.com;wpatterson@classicmodelcars.com;jfirrelli@classicmodelcars.com;mpatterso@classicmodelcars.com;dmurphy@classicmodelcars.com;\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a MySQL cursor to process a result set row by row individually.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"535\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/\"\n\t\t\t\tdata-post-title=\"MySQL Cursor\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"535\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/\"\n\t\t\t\tdata-post-title=\"MySQL Cursor\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Shows you how to use MySQL cursor in stored procedures to loop over a result set and process each row at a time.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":518,"menu_order":20,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-535","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL Cursor<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Cursor\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-07T02:14:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/\",\"name\":\"MySQL Cursor\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2009\\\/12\\\/mysql-cursor.png\",\"datePublished\":\"2009-12-27T13:26:43+00:00\",\"dateModified\":\"2024-02-07T02:14:11+00:00\",\"description\":\"In this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2009\\\/12\\\/mysql-cursor.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2009\\\/12\\\/mysql-cursor.png\",\"width\":612,\"height\":88,\"caption\":\"mysql cursor\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/sql-cursor-in-stored-procedures\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Stored Procedures\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-stored-procedure\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL Cursor\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL Cursor","description":"In this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Cursor","og_description":"In this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-02-07T02:14:11+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/","url":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/","name":"MySQL Cursor","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png","datePublished":"2009-12-27T13:26:43+00:00","dateModified":"2024-02-07T02:14:11+00:00","description":"In this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-cursor.png","width":612,"height":88,"caption":"mysql cursor"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/sql-cursor-in-stored-procedures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Stored Procedures","item":"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/"},{"@type":"ListItem","position":3,"name":"MySQL Cursor"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/535\/revisions"}],"predecessor-version":[{"id":14635,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/535\/revisions\/14635"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/518"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}