Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't really need PL/SQL for this; you can use <code>regexp_substr</code>:</p> <pre><code>select count(distinct value) from ( select regexp_substr(interfaceline, '[^|]+', 1, :arrayPos) as value from inttestintrecs where sessionid between :loSessId and :hiSessId ); </code></pre> <p>... where the bind variables are the values you were passing to the function. If you really want a function for that you can just wrap the query (updated with tbone's <code>replace</code> trick to deal with null values):</p> <pre><code>create or replace function DistinctInterfacelineValues( inArrayPos NUMBER, inSessionIdMIN NUMBER, inSessionIdMAX NUMBER) RETURN NUMBER AS distinctCount NUMBER; BEGIN select count(distinct value) into distinctCount from ( select trim(regexp_substr(replace(interfaceline, '|', ' |'), '[^|]+', 1, inArrayPos)) as value from inttestintrecs where sessionid between inSessionIdMIN and inSessionIdMAX ); RETURN distinctCount; END; / </code></pre> <p><a href="http://sqlfiddle.com/#!4/85409/1" rel="nofollow">SQL Fiddle demo</a>.</p> <hr> <p>Within the nested loop version you posted, you <em>could</em> maintain a PL/SQL table indexed by the value rather than a number:</p> <pre><code>... TYPE table_type IS TABLE OF NUMBER INDEX BY VARCHAR2(100); tempTable table_type; tempValue VARCHAR2(100); ... IF i = inArrayPos THEN tempValue := regexp_substr(int_record.interfaceline, '[^|]+', 1, i); tempTable(tempValue) := 1; END IF; ... RETURN tempTable.count; ... </code></pre> <p><a href="http://sqlfiddle.com/#!4/76073/1" rel="nofollow">Another Fiddle</a> just for fun, but there's really no point having all the PL/SQL loop overhead, particularly with the <code>regexp_substr</code> in the middle anyway. You could increment the count held in <code>tempTable</code> for each value so you knew how many of each you'd seen, and you could get the actual values from <code>tempTable</code> too... but you can get those from the pure SQL version just as easily (selecting <code>value, count(*)</code> and <code>distinct value</code>).</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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