{"id":14423,"date":"2020-04-18T15:17:18","date_gmt":"2020-04-18T15:17:18","guid":{"rendered":"https:\/\/ittutorial.org\/?p=14423"},"modified":"2020-04-27T23:13:01","modified_gmt":"2020-04-27T23:13:01","slug":"pl-sql-for-practitioners-1-block-structure","status":"publish","type":"post","link":"https:\/\/ittutorial.org\/pl-sql-for-practitioners-1-block-structure\/","title":{"rendered":"PL\/SQL For Practitioners &#8211; #1 Block Structure"},"content":{"rendered":"<p>Hi,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">PL\/SQL is a block-structured language whose code is organized into blocks. A PL\/SQL block consists of three sections, <strong>declaration (non-required)<\/strong>, <strong>executable (required)<\/strong>, and <strong>exception-handling (non-required)<\/strong> sections. All kinds of structures that a developer might create in PL\/SQL such as procedures, functions, packages, and types are based on this primary structure. It will vary from one object to another, but they will be very similar.<\/p>\n<p>&nbsp;<\/p>\n<h2>The basic structure<\/h2>\n<pre>DECLARE --(optional)\r\n  -- Here is where you can declare variables in general when needed.\r\n\r\nBEGIN      --(required)\r\n  -- Executable code\r\n\r\n[EXCEPTION -- (optional)\r\n  -- error-handling if needed\r\nEND;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Example 1<\/h2>\n<p style=\"text-align: justify\">This is a simple anonymous block that will show the current date.<\/p>\n<pre>DECLARE \r\n  CUR_DATE DATE; \r\nBEGIN \r\n  CUR_DATE := sysdate; \r\n  DBMS_OUTPUT.PUT_LINE('THE CURRENT DATE IS: ' || CUR_DATE); \r\nEND;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14433\" src=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-11_29_33-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox-300x130.png\" alt=\"\" width=\"704\" height=\"305\" srcset=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-11_29_33-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox-300x130.png 300w, https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-11_29_33-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox-768x333.png 768w, https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-11_29_33-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox.png 785w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/p>\n<h2>Example 2<\/h2>\n<p style=\"text-align: justify\">Another example might be the following script. It is responsible to check whether the city has more than 1000000 inhabitants.<\/p>\n<pre>DECLARE\r\n \u00a0 \u00a0COUNT_BIG_CITIES NUMBER := 0;\r\nBEGIN\r\n \u00a0 \u00a0FOR C IN (SELECT POPULATION FROM CITY)\r\n \u00a0 \u00a0LOOP\r\n \u00a0 \u00a0 \u00a0IF C.POPULATION &gt; 1000000 THEN\r\n \u00a0 \u00a0 \u00a0 \u00a0COUNT_BIG_CITIES := COUNT_BIG_CITIES + 1;\r\n \u00a0 \u00a0 \u00a0END IF;\r\n \u00a0 \u00a0END LOOP;\r\n \u00a0 \u00a0DBMS_OUTPUT.PUT_LINE('There are ' || COUNT_BIG_CITIES || ' big cities'); \r\nEND;<\/pre>\n<p><strong>Result<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14437\" src=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-16_55_16-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox-300x189.png\" alt=\"\" width=\"656\" height=\"413\" srcset=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-16_55_16-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox-300x189.png 300w, https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/2020-04-17-16_55_16-ODI-12c-Getting-Started-VM-OTN-122131-Running-Oracle-VM-VirtualBox.png 670w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Well, this is only a small example of what we can do with blocks in PL\/SQL. This is the central structure for almost everything we do while developing for Oracle PL\/SQL.<\/p>\n<p style=\"text-align: justify\">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=\"dhImA64k9G\"><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=HJgfqLgozU#?secret=dhImA64k9G\" data-secret=\"dhImA64k9G\" 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. I&#8217;ll be happy to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, &nbsp; PL\/SQL is a block-structured language whose code is organized into blocks. A PL\/SQL block consists of three sections, declaration (non-required), executable (required), and exception-handling (non-required) sections. All kinds of structures that a developer might create in PL\/SQL such as procedures, functions, packages, and types are based on this primary structure. It will vary &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":[9420,3617,9419,8755],"class_list":["post-14423","post","type-post","status-publish","format-standard","has-post-thumbnail","","category-oracle","category-oracle-sql","category-pl-sql","tag-oracle-block","tag-oracle-pl-sql","tag-oracle-pl-sql-block","tag-pl-sql-block"],"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\/14423","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=14423"}],"version-history":[{"count":16,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/14423\/revisions"}],"predecessor-version":[{"id":14786,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/14423\/revisions\/14786"}],"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=14423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/categories?post=14423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/tags?post=14423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}