title: What is the best way of handling a temporary LOB in PL/SQL
body: <p>When it comes to temporary LOBs in PL/SQL, i.e. ones that won't be stored in the database, what's the best/proper way to create and dispose of them? I'm especially thinking of <code>dbms_lob.createtemporary</code> and <code>dbms_lob.free temporary</code>.</p>
  
  <p>For example, how are these cases different? </p>
  
  <pre><code>-- CASE 1
  DECLARE
      mylob CLOB;
  BEGIN
      mylob = 'foo';
  END;
  
  --CASE 2
  DECLARE
      mylob CLOB;
  BEGIN
      dbms_lob.createtemporary( mylob, TRUE );
      mylob = 'foo';
      dbms_lob.freetemporary( mylob );
  END;
  </code></pre>
  
  <p>Say I have a function that returns a LOB. Should I create a temporary lob in the function and make it the caller's responsibility to free it?</p>
  
  <pre><code>DECLARE
      myclob2 CLOB;
  
      FUNCTION myfn RETURN CLOB IS
          myclob1 CLOB := '';
      BEGIN
          dbms_lob.createtemporary( myclob1, TRUE );
          myclob1 := 'foo';
          RETURN myclob1;
      END myfn;
  BEGIN
      myclob2 := myfn;
      dbms_output.put_line( myclob2 );
      dbms_lob.freetemporary( myclob2 );
  END;
  </code></pre>
  
  <p>Edit:
  I guess one of the things I'm especially interested in is how to properly dispose of the LOB so that it doesn't hang around until the session ends.</p>