{"id":14441,"date":"2020-04-18T15:19:40","date_gmt":"2020-04-18T15:19:40","guid":{"rendered":"https:\/\/ittutorial.org\/?p=14441"},"modified":"2020-04-27T23:15:20","modified_gmt":"2020-04-27T23:15:20","slug":"pl-sql-for-practitioners-2-implicit-cursor-for-loop-statement","status":"publish","type":"post","link":"https:\/\/ittutorial.org\/pl-sql-for-practitioners-2-implicit-cursor-for-loop-statement\/","title":{"rendered":"PL\/SQL For Practitioners &#8211; #2 Implicit Cursor FOR LOOP Statement"},"content":{"rendered":"<p>Hi,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Other structures when programming in PL\/SQL are the <strong>FOR LOOP<\/strong> and <strong>CURSOR<\/strong>. In this post, I&#8217;m going to show how to use both of them together.<\/p>\n<p style=\"text-align: justify\">Well, there are plenty of ways to create a loop in PL\/SQL, one of the most common using an <strong>implicit cursor<\/strong>. This is encouraged when the query is not to much complex. I like it because it is very simple, I don&#8217;t need to worry to open, fetch and close each cursor. Everything is controlled by the database engine.<\/p>\n<p>&nbsp;<\/p>\n<h2>The basic structure<\/h2>\n<p style=\"text-align: justify\">When using an implicit cursor, you don&#8217;t have to OPEN CURSOR, FETCH CURSOR and CLOSE CURSOR. When the loop is finished, the current cursor is over automatically.<\/p>\n<pre>FOR &lt;Loop_index&gt; IN (&lt;Select_statement&gt;)\r\n\r\nLOOP\r\n\r\n   &lt;Executable_statements&gt;;\r\n\r\nEND LOOP;<\/pre>\n<p>&nbsp;<\/p>\n<h2>Example<\/h2>\n<p style=\"text-align: justify\">Please, use the file <a href=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/create_and_populate_tables_city_and_country.txt\">create_and_populate_tables_city_and_country<\/a> file to create and populate the tables used in this article<\/p>\n<pre>BEGIN\r\n\r\nFOR C1 IN (select ci.name as name\r\n           from city ci\r\n              , country co\r\n          where ci.country_id = co.id\r\n            and co.iso2 = 'BR')\r\nLOOP\r\n\r\n   dbms_output.put_line(C1.name);\r\n\r\nEND LOOP;\r\n\r\nEND;<\/pre>\n<p><strong>Result<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%;height: 447px\">\n<tbody>\n<tr style=\"height: 447px\">\n<td style=\"width: 100%;height: 447px\">S\u00e3o Paulo<br \/>\nRio de Janeiro<br \/>\nBelo Horizonte<br \/>\nPorto Alegre<br \/>\nBras\u00edlia<br \/>\nRecife<br \/>\nFortaleza<br \/>\nSalvador<br \/>\nCuritiba<br \/>\nCampinas<br \/>\nBel\u00e9m<br \/>\nGoi\u00e2nia<br \/>\nManaus<br \/>\nSantos<br \/>\nVit\u00f3ria<br \/>\nNiter\u00f3i<br \/>\nVila Velha<br \/>\nVila Velha<br \/>\nMacei\u00f3<br \/>\nNatal<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>Dynamic values<\/h2>\n<p>It is also possible to add dynamic values to filter your data in the query inside of the cursor. Check out the following example.<\/p>\n<pre>DECLARE\r\n  ISO VARCHAR2(3);\r\nBEGIN ISO := 'BO'; \r\n   FOR C1 IN (select ci.name as name \r\n               from city ci ,\r\n                   country co \r\n              where ci.country_id = co.id and co.iso2 = ISO) \r\n   LOOP \r\n      dbms_output.put_line(C1.name); \r\n   END LOOP; \r\nEND;<\/pre>\n<p><strong>Result<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 100%\">Santa Cruz<br \/>\nLa Paz<br \/>\nSucre<br \/>\nCochabamba<br \/>\nOruro<br \/>\nSanta Ana de Yacuma<br \/>\nQuillacollo<br \/>\nPotos\u00ed<br \/>\nTarija<br \/>\nTrinidad<br \/>\nCobija<br \/>\nSanta Cruz de la Sierra<br \/>\nMontero<br \/>\nRiberalta<br \/>\nVillaz\u00f3n<br \/>\nBermejo<br \/>\nGuayaramer\u00edn<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Well, this is pretty much enough to you write your own implicit cursors. Many more could be written, but I&#8217;m trying to keep these posts as simple as possible to be practical.<\/p>\n<p>&nbsp;<\/p>\n<p>This post is part of a series about the most common structures in PL\/SQL. You can check the rest of them in the link below.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"IE0RKBa3mP\"><p><a href=\"https:\/\/ittutorial.org\/pl-sql-for-real-world-8-features-that-you-have-to-know\/\">PL\/SQL for real world &#8211; 8 features that you have to know<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;PL\/SQL for real world &#8211; 8 features that you have to know&#8221; &#8212; IT Tutorial\" src=\"https:\/\/ittutorial.org\/pl-sql-for-real-world-8-features-that-you-have-to-know\/embed\/#?secret=Ise2vghcN3#?secret=IE0RKBa3mP\" data-secret=\"IE0RKBa3mP\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>Please write down in the comments any further questions you may have, or get in touch via e-mail and LinkedIn. I&#8217;ll be happy to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, &nbsp; Other structures when programming in PL\/SQL are the FOR LOOP and CURSOR. In this post, I&#8217;m going to show how to use both of them together. Well, there are plenty of ways to create a loop in PL\/SQL, one of the most common using an implicit cursor. This is encouraged when the query &hellip;<\/p>\n","protected":false},"author":10477,"featured_media":14709,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1994,3019,3647],"tags":[8775,1120,3617,3618,8776,8772,8774],"class_list":["post-14441","post","type-post","status-publish","format-standard","has-post-thumbnail","","category-oracle","category-oracle-sql","category-pl-sql","tag-implicit-for-loop-statement","tag-oracle","tag-oracle-pl-sql","tag-pl-sql","tag-pl-sql-cursor-for-loop","tag-pl-sql-for","tag-pl-sql-implicit-cursor-for-loop"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/PLSQL_ICO.png","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/14441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/users\/10477"}],"replies":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/comments?post=14441"}],"version-history":[{"count":9,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/14441\/revisions"}],"predecessor-version":[{"id":14627,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/14441\/revisions\/14627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/media\/14709"}],"wp:attachment":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/media?parent=14441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/categories?post=14441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/tags?post=14441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}