{"id":1994,"date":"2025-05-27T01:10:59","date_gmt":"2025-05-27T05:10:59","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1994"},"modified":"2025-05-22T01:16:37","modified_gmt":"2025-05-22T05:16:37","slug":"how-to-fix-pls-00428-an-into-clause-is-expected-in-this-select-statement","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/how-to-fix-pls-00428-an-into-clause-is-expected-in-this-select-statement\/","title":{"rendered":"How to Fix PLS-00428: An INTO Clause Is Expected in This SELECT Statement"},"content":{"rendered":"\n<p>In PL\/SQL, when you execute a <code>SELECT<\/code> statement inside a PL\/SQL block (such as in a procedure, function, or anonymous block), you must <strong>fetch the selected data into variables<\/strong> using the <code>INTO<\/code> clause.<\/p>\n\n\n\n<p>The error:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>PLS-00428: an INTO clause is expected in this SELECT statement<br><\/code><\/pre>\n\n\n\n<p>is triggered when PL\/SQL expects you to store the result of a <code>SELECT<\/code> query in a variable, but you forget to include the <code>INTO<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Why This Error Occurs<\/strong><\/h2>\n\n\n\n<p>In <strong>SQL<\/strong>, you can write:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name FROM employees WHERE employee_id = 100;<br><\/code><\/pre>\n\n\n\n<p>and it simply displays the result.<\/p>\n\n\n\n<p>But in <strong>PL\/SQL<\/strong>, you must write:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name INTO v_name FROM employees WHERE employee_id = 100;<br><\/code><\/pre>\n\n\n\n<p>because PL\/SQL requires a target for the result \u2014 a variable to store the value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Example of the Error<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Code That Triggers PLS-00428<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>BEGIN<br>   SELECT first_name FROM employees WHERE employee_id = 100;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Error Output<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>PLS-00428: an INTO clause is expected in this SELECT statement<br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Correct Usage with INTO Clause<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Fixed Version<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>DECLARE<br>   v_name employees.first_name%TYPE;<br>BEGIN<br>   SELECT first_name INTO v_name FROM employees WHERE employee_id = 100;<br>   DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_name);<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Employee name: Steven<br><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The <code>INTO<\/code> clause tells PL\/SQL to store the result of the query into the variable <code>v_name<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Handling Multiple Columns<\/strong><\/h2>\n\n\n\n<p>If your <code>SELECT<\/code> returns multiple columns, you need multiple variables in the <code>INTO<\/code> clause.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Multiple Columns<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>DECLARE<br>   v_fname employees.first_name%TYPE;<br>   v_lname employees.last_name%TYPE;<br>BEGIN<br>   SELECT first_name, last_name<br>   INTO v_fname, v_lname<br>   FROM employees<br>   WHERE employee_id = 100;<br><br>   DBMS_OUTPUT.PUT_LINE('Full Name: ' || v_fname || ' ' || v_lname);<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Using INTO with ROWTYPE<\/strong><\/h2>\n\n\n\n<p>If you&#8217;re selecting all columns, you can use <code>%ROWTYPE<\/code> for cleaner code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>DECLARE<br>   v_emp employees%ROWTYPE;<br>BEGIN<br>   SELECT * INTO v_emp FROM employees WHERE employee_id = 100;<br>   DBMS_OUTPUT.PUT_LINE('Employee Email: ' || v_emp.email);<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. When You Don\u2019t Need an INTO Clause<\/strong><\/h2>\n\n\n\n<p>The only time you <strong>don\u2019t use an INTO clause<\/strong> is when you&#8217;re using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>cursor FOR loop<\/strong><\/li>\n\n\n\n<li><strong>Dynamic SQL<\/strong> with <code>EXECUTE IMMEDIATE<\/code><\/li>\n\n\n\n<li><strong>SELECT INTO<\/strong> as a standalone SQL query (outside PL\/SQL)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Cursor FOR Loop Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>BEGIN<br>   FOR rec IN (SELECT employee_id, last_name FROM employees WHERE department_id = 10) LOOP<br>      DBMS_OUTPUT.PUT_LINE(rec.employee_id || ' - ' || rec.last_name);<br>   END LOOP;<br>END;<br>\/<br><\/code><\/pre>\n\n\n\n<p>Notice that there&#8217;s <strong>no INTO clause here<\/strong> \u2014 PL\/SQL automatically handles it via the loop variable <code>rec<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. Common Pitfalls and Fixes<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Mistake<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Writing <code>SELECT ...<\/code> alone in PL\/SQL<\/td><td>Add <code>INTO<\/code> clause with variable(s)<\/td><\/tr><tr><td>Forgetting <code>INTO<\/code> for multi-column SELECT<\/td><td>Include all target variables in same order<\/td><\/tr><tr><td>Using SELECT in <code>BEGIN...END;<\/code> block without INTO<\/td><td>Use cursor or dynamic SQL if needed<\/td><\/tr><tr><td>SELECT returns more than one row<\/td><td>Use <code>SELECT INTO<\/code> only when exactly one row is expected or use <code>CURSOR<\/code>\/<code>LOOP<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. Summary<\/strong><\/h2>\n\n\n\n<p><strong>PLS-00428<\/strong> is a very common beginner mistake in PL\/SQL programming. To resolve it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always include an <code>INTO<\/code> clause when using <code>SELECT<\/code> inside PL\/SQL blocks.<\/li>\n\n\n\n<li>Ensure the number and type of variables in the <code>INTO<\/code> clause match the <code>SELECT<\/code> list.<\/li>\n\n\n\n<li>Use <code>%TYPE<\/code> and <code>%ROWTYPE<\/code> to ensure compatibility with column types.<\/li>\n\n\n\n<li>Use cursor loops or collections for multi-row queries.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Want to Avoid This Error Completely?<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use cursor <code>FOR<\/code> loops for queries that return multiple rows.<\/li>\n\n\n\n<li>Always test queries in SQL first, then add variables and <code>INTO<\/code> clause in PL\/SQL.<\/li>\n\n\n\n<li>Add exception handling to capture cases where rows are missing or duplicated.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In PL\/SQL, when you execute a SELECT statement inside a PL\/SQL block (such as in a procedure, function, or anonymous block), you must fetch the selected data into variables using the INTO clause. The error: PLS-00428: an INTO clause is expected in this SELECT statement is triggered when PL\/SQL expects you to store the result [&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-1994","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\/1994","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=1994"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1994\/revisions"}],"predecessor-version":[{"id":1997,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1994\/revisions\/1997"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}