{"id":157,"date":"2019-09-24T07:56:31","date_gmt":"2019-09-24T07:56:31","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=157"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"cursor","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/cursor\/","title":{"rendered":"Cursor"},"content":{"rendered":"\n<p>SQL works based on set e.g.,&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>&nbsp;statement returns a set of rows which is called a result set. However, sometimes, you may want to process a data set on a row by row basis. This is where cursors come into play.<\/p>\n\n\n\n<h2>What is a database cursor<\/h2>\n\n\n\n<p>A database cursor is an object that enables traversal over the rows of a result set. It allows you to process individual row returned by a query.<\/p>\n\n\n\n<h2>SQL Server cursor life cycle<\/h2>\n\n\n\n<p>These are steps for using a cursor:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Cursor.png\" alt=\"SQL Server Cursor\" class=\"wp-image-1731\"\/><\/figure>\n\n\n\n<p>First, declare a cursor.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>DECLARE cursor_name CURSOR&nbsp;&nbsp;&nbsp;&nbsp;FOR select_statement;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To declare a cursor, you specify its name after the&nbsp;<code>DECLARE<\/code>&nbsp;keyword with the&nbsp;<code>CURSOR<\/code>&nbsp;data type and provide a&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>&nbsp;statement that defines the result set for the cursor.<\/p>\n\n\n\n<p>Next, open and popular the cursor by executing the&nbsp;<code>SELECT<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>OPEN cursor_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Then, fetch a row from the cursor into one or more&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\">variables<\/a>:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>FETCH NEXT FROM cursor INTO variable_list;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>SQL Server provides the&nbsp;<code>@@FETCHSTATUS<\/code>&nbsp;function that returns the status of the last cursor&nbsp;<code>FETCH<\/code>&nbsp;statement executed against the cursor; If&nbsp;<code>@@FETCHSTATUS<\/code>&nbsp;returns 0, meaning the&nbsp;<code>FETCH<\/code>&nbsp;statement was successful. You can use the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\">WHILE<\/a><\/code>&nbsp;statement to fetch all rows from the cursor as shown in the following code:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>WHILE @@FETCH_STATUS = 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FETCH NEXT FROM cursor_name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>After that, close the cursor:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>CLOSE cursor_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Finally, deallocate the cursor:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DEALLOCATE cursor_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2>SQL Server cursor example<\/h2>\n\n\n\n<p>First, declare two variables to hold product name and list price, and a cursor to hold the result of a query that selects product name and list price from the&nbsp;<code>production.products<\/code>&nbsp;table:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910<\/td><td>DECLARE &nbsp;&nbsp;&nbsp;&nbsp;@product_name VARCHAR(MAX), &nbsp;&nbsp;&nbsp;&nbsp;@list_price&nbsp;&nbsp; DECIMAL;&nbsp;DECLARE cursor_product CURSORFOR SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&nbsp;&nbsp;&nbsp;&nbsp;FROM &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Next, open the cursor:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>OPEN cursor_product;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Then, fetch each row from the cursor and print out the product name and list price:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011<\/td><td>FETCH NEXT FROM cursor_product INTO &nbsp;&nbsp;&nbsp;&nbsp;@product_name, &nbsp;&nbsp;&nbsp;&nbsp;@list_price;&nbsp;WHILE @@FETCH_STATUS = 0&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT @product_name + CAST(@list_price AS varchar);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FETCH NEXT FROM cursor_product INTO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product_name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@list_price;&nbsp;&nbsp;&nbsp;&nbsp;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>After that, close the cursor.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>CLOSE cursor_product;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Finally, deallocate the cursor to release it.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DEALLOCATE cursor_product;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The following code snippets put everything together:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213141516171819202122232425262728<\/td><td>DECLARE &nbsp;&nbsp;&nbsp;&nbsp;@product_name VARCHAR(MAX), &nbsp;&nbsp;&nbsp;&nbsp;@list_price&nbsp;&nbsp; DECIMAL;&nbsp;DECLARE cursor_product CURSORFOR SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&nbsp;&nbsp;&nbsp;&nbsp;FROM &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products;&nbsp;OPEN cursor_product;&nbsp;FETCH NEXT FROM cursor_product INTO &nbsp;&nbsp;&nbsp;&nbsp;@product_name, &nbsp;&nbsp;&nbsp;&nbsp;@list_price;&nbsp;WHILE @@FETCH_STATUS = 0&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT @product_name + CAST(@list_price AS varchar);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FETCH NEXT FROM cursor_product INTO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product_name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@list_price;&nbsp;&nbsp;&nbsp;&nbsp;END;&nbsp;CLOSE cursor_product;&nbsp;DEALLOCATE cursor_product;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here is the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Cursor-Example.png\" alt=\"SQL Server Cursor Example\" class=\"wp-image-1732\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>SQL works based on set e.g.,&nbsp;SELECT&nbsp;statement returns a set of rows which is called a result set. However, sometimes, you may want to process a data set on a row by row basis. This is&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/157"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":158,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/157\/revisions\/158"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}