Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing bind variables in SQL Plus with more than one row returned?
    primarykey
    data
    text
    <p>This is a stupid problem, but I can't seem to get around it. I have a query that's causing trouble in an OCI program, so I want to run it manually in SQL*Plus to check if there is any difference there. This is the query:</p> <pre><code>select e.label as doc_name, e.url, i.item_id, 'multi' as form_type from cr_items i, cr_extlinks e where i.parent_id = :comment_id and e.extlink_id = i.item_id UNION select null as doc_name, utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(r.content, 2000, 1)) as url, r.item_id, 'single' as form_type from cr_revisions r where r.revision_id = ( select content_item.get_latest_revision(:comment_id) from dual); end; </code></pre> <p>I wanted to bind the comment_id to the value 3052753, so I did the following:</p> <pre><code> DECLARE comment_id number := 3052753; BEGIN select e.label , e.url, i.item_id, 'multi' from cr_items i, cr_extlinks e where i.parent_id = :comment_id and e.extlink_id = i.item_id UNION select null , utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(r.content, 2000, 1)) as url, r.item_id, 'single' from cr_revisions r where r.revision_id = ( select content_item.get_latest_revision(:comment_id) from dual); END; / </code></pre> <p>which gives this error:</p> <pre><code>ORA-06550: line 4, column 1: PLS-00428: an INTO clause is expected in this SELECT statement </code></pre> <p>Now, I'm already unhappy because I don't want to be radically changing this query, but anyway I forge ahead and come up with this (INTO and UNIONs don't go together so smoothly):</p> <pre><code>DECLARE comment_id number := 3052753; x_label VARCHAR2(50); x_url VARCHAR2(500); x_item number; x_thing VARCHAR2(50); BEGIN select label, url, item_id, thing into x_label, x_url, x_item, x_thing from ( select e.label , e.url, i.item_id, 'multi' as thing from cr_items i, cr_extlinks e where i.parent_id = :comment_id and e.extlink_id = i.item_id UNION select null , utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(r.content, 2000, 1)) as url, r.item_id, 'single' as thing from cr_revisions r where r.revision_id = ( select content_item.get_latest_revision(:comment_id) from dual)) ; END; / </code></pre> <p>but now, of course because I'm returning more than 1 row, I get the utterly predictable</p> <pre><code>ORA-01422: exact fetch returns more than requested number of rows </code></pre> <p>Now, I can go ahead and start using cursors etc, but my little query is getting more and more distorted from its original self. All I wanted to do was to check if the query ran ok with that value of comment_id. Of course, I can just hardcode the comment_id into the query, and that works fine. But it also works fine in the OCI so I'm to reproduce in SQL*PLus the issue with bind variables that I'm seeing in the OCI code. But why is it such a struggle to do this in SQL*Plus? Have I missed something really obvious?</p> <p>Database is Oracle 10.2.0.1.0 - 64bit running on Red Hat Enterprise Linux ES release 4 (Nahant Update 8)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload