{"id":1989,"date":"2025-05-25T00:59:42","date_gmt":"2025-05-25T04:59:42","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1989"},"modified":"2025-05-22T01:02:07","modified_gmt":"2025-05-22T05:02:07","slug":"solving-pls-00364-loop-index-variable-must-be-an-integer","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/solving-pls-00364-loop-index-variable-must-be-an-integer\/","title":{"rendered":"Solving PLS-00364: Loop Index Variable <name> Must Be an Integer<\/name>"},"content":{"rendered":"\n<p>In PL\/SQL, <code>FOR<\/code> loops are a powerful control structure used to iterate over a defined range of numbers or rows. However, this construct has specific syntactic rules. One common error developers encounter is:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>PLS-00364: Loop index variable <code>&lt;name&gt;<\/code> must be an integer<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>This article explains <strong>why this error occurs<\/strong>, provides <strong>examples<\/strong>, and offers <strong>clear solutions<\/strong> to fix it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Causes PLS-00364?<\/strong><\/h2>\n\n\n\n<p>The error occurs when the loop index variable in a <code>FOR<\/code> loop is either:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explicitly declared<\/strong> as a non-integer type (like <code>NUMBER<\/code>, <code>FLOAT<\/code>, <code>DECIMAL<\/code>, <code>VARCHAR2<\/code>, etc.)<\/li>\n\n\n\n<li>Or, when the range provided in the loop is <strong>not an integer range<\/strong><\/li>\n<\/ul>\n\n\n\n<p>PL\/SQL requires that <code>FOR<\/code> loop index variables are always <strong>implicitly<\/strong> created and treated as <strong>integers<\/strong>. You cannot use decimal values or user-declared non-integer loop counters in a <code>FOR<\/code> loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples That Produce PLS-00364<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Loop Range with Decimal Values<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>BEGIN<br>   FOR i IN 1.1 .. 5.1 LOOP<br>      DBMS_OUTPUT.PUT_LINE(i);<br>   END LOOP;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p><strong>Error Message:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>PLS-00364: loop index variable 'I' must be an integer<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Loop Index Variable Declared as NUMBER<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>DECLARE<br>   i NUMBER := 1;<br>BEGIN<br>   FOR i IN 1 .. 5 LOOP<br>      DBMS_OUTPUT.PUT_LINE(i);<br>   END LOOP;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p><strong>Why this fails:<\/strong> Even though <code>i<\/code> is declared as <code>NUMBER<\/code>, the compiler does not allow this conflict because the loop control variable is implicitly managed by the <code>FOR<\/code> loop construct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Correct Usage: Use Integer Values Only<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Solution 1: Use Integer Range Without Declaring the Index<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>BEGIN<br>   FOR i IN 1 .. 5 LOOP<br>      DBMS_OUTPUT.PUT_LINE('Value: ' || i);<br>   END LOOP;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p>This works correctly because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The index <code>i<\/code> is <strong>implicitly declared<\/strong><\/li>\n\n\n\n<li>The range is a pair of <strong>integer literals<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Solution 2: Use Integer Variables in Range<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>DECLARE<br>   v_start INTEGER := 1;<br>   v_end   INTEGER := 5;<br>BEGIN<br>   FOR i IN v_start .. v_end LOOP<br>      DBMS_OUTPUT.PUT_LINE('Value: ' || i);<br>   END LOOP;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p>Again, the range resolves to integers, and the loop variable <code>i<\/code> is not explicitly declared.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Solution 3: Use WHILE Loop for Non-Integer Increments<\/h3>\n\n\n\n<p>If your logic involves non-integer increments (e.g., 0.5 or 2.7), a <code>FOR<\/code> loop cannot be used. Instead, use a <code>WHILE<\/code> loop with a floating-point variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>DECLARE<br>   v_counter NUMBER := 1.5;<br>BEGIN<br>   WHILE v_counter &lt;= 5.0 LOOP<br>      DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);<br>      v_counter := v_counter + 0.5;<br>   END LOOP;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p><strong>Why this works:<\/strong> <code>WHILE<\/code> loops allow more flexibility with data types and control logic, unlike <code>FOR<\/code> loops that enforce integer control variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary: Do&#8217;s and Don&#8217;ts<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th><strong>Do<\/strong><\/th><th><strong>Don\u2019t<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Use implicit loop variables (<code>FOR i IN...<\/code>)<\/td><td>Declare loop index as <code>NUMBER<\/code>, <code>FLOAT<\/code>, etc.<\/td><\/tr><tr><td>Use <code>INTEGER<\/code>, <code>PLS_INTEGER<\/code>, or literals<\/td><td>Use decimals like <code>1.1 .. 5.5<\/code> in loop range<\/td><\/tr><tr><td>Use <code>WHILE<\/code> loop for decimal counters<\/td><td>Expect <code>FOR<\/code> loop to handle non-integer logic<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <strong>PLS-00364<\/strong> error is a strict PL\/SQL constraint that enforces loop index variables to be <strong>integers only<\/strong> within <code>FOR<\/code> loops. To fix this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Let Oracle implicitly create the index variable<\/li>\n\n\n\n<li>Ensure loop bounds are integers<\/li>\n\n\n\n<li>Use <code>WHILE<\/code> loops if your logic needs floating-point arithmetic<\/li>\n<\/ul>\n\n\n\n<p>By following these guidelines, your loops will compile cleanly and behave as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PL\/SQL, FOR loops are a powerful control structure used to iterate over a defined range of numbers or rows. However, this construct has specific syntactic rules. One common error developers encounter is: PLS-00364: Loop index variable &lt;name&gt; must be an integer This article explains why this error occurs, provides examples, and offers clear solutions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[515],"tags":[529],"class_list":["post-1989","post","type-post","status-publish","format-standard","hentry","category-oracle","tag-pl-sql"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1989"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1989\/revisions"}],"predecessor-version":[{"id":1990,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1989\/revisions\/1990"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}