title: Oracle: Get a query to always return exactly one row, even when there's no data to be found
body: <p>I have a query like this:</p>
  
  <pre><code>   select data_name
     into v_name
     from data_table
     where data_table.type = v_t_id
  </code></pre>
  
  <p>Normally, this query should return exactly one row. When there's no match on <code>v_t_id</code>, the program fails with a "No data found" exception.</p>
  
  <p>I know I <em>could</em> handle this in PL/SQL, but I was wondering if there's a way to do this only in a query. As a test, I've tried:</p>
  
  <pre><code>select case
             when subq.data_name is null then
              'UNKNOWN'
             else
              subq.data_name
         end
  from (select data_name
         from data_table
         where data_table.type = '53' /*53 does not exist, will result in 0 rows. Need fix this...*/
         ) subq;
  </code></pre>
  
  <p>...but this will obviously not work (because <code>subq</code> being empty is not the same as <code>subq.data_name is null</code>). Is this even possible or should I just check in my PL/SQL solution?</p>
  
  <p>(oracle 10g)</p>