title: String variable returns an empty value
body: <p>Below is a PB function script that basically creates a reference number. This script is working in the production environment but there are cases that the string variable(ls_ivno) returns an empty value and I suspect that there is a deadlock which happens during this process.</p>
  
  <p>Seeking help to trace if where is the possible problem on the this script.</p>
  
  <pre><code>String ls_branch, ls_gcode1, ls_ivprint, ls_ivno, ls_keyname
  long ll_kcount
  
  Select remarks11, remarks12, remarks13
  Into :ls_branch, :ls_gcode1, :ls_ivprint
  From ordersext
  Where orderkey = :arg_orderkey;
  
  ls_branch = trim(ls_branch)
  ls_gcode1 = trim(ls_gcode1)
  ls_ivprint = trim(ls_ivprint)
  
  choose case ls_ivprint
      case 'SIV' //format1
          ls_ivno =  ls_branch + right(string(today(),"YY"),1) + "U" + ls_gcode1
      case 'IIV' //format2
          ls_ivno =  ls_branch + "Q" + string(today(),"YY") 
     case else  //blank
          ls_ivno = ' '
  end choose
  
  if ls_ivno &lt;&gt; ' ' then
          if ls_ivprint = 'SIV' THEN
              declare ncounter_invprn cursor for
              select keyname, keycount
              from ncounter
              where filler01 = 'invprn'
              order by keyname;
  
              open ncounter_invprn;
                  fetch ncounter_invprn into :ls_keyname, :ll_kcount;
                  do while sqlca.sqlcode = 0
                      setpointer(hourglass!)  
                      if ll_kcount &lt;&gt; 9999 then
                          ls_ivno += right(trim(ls_keyname),1) + f_get_ncounter(ls_keyname, 4)
                          close ncounter_invprn;
                      end if  
                  fetch ncounter_invprn into :ls_keyname, :ll_kcount;
                  loop
              close ncounter_invprn ;
  
          ELSE //IIV
              declare ncounter_invprn_iiv cursor for
              select keyname, keycount
              from ncounter
              where filler01 = 'invprn_iiv'
              order by keyname;
  
              open ncounter_invprn_iiv;
                  fetch ncounter_invprn_iiv into :ls_keyname, :ll_kcount;
                  do while sqlca.sqlcode = 0
                      setpointer(hourglass!)  
                      if ll_kcount &lt;&gt; 9999 then
                          ls_ivno += right(trim(ls_keyname),1) + f_get_ncounter(ls_keyname, 4)
                          close ncounter_invprn_iiv;
                      end if  
                  fetch ncounter_invprn_iiv into :ls_keyname, :ll_kcount;
                  loop
              close ncounter_invprn_iiv;
  
          END IF
  end if      
  
  return ls_ivno
  </code></pre>