title: RPGLE Dynamic SQL with Select clause does not work
body: <p>After reading several articles about SQLRPGLE and retrieving data and storing them in data structure arrays, I came up with dynamic sql statements.</p>
  
  <p>This works fine as long as I am using these dynamic to-replace fields for my where condition. But as soo as I am using these ? parameter in the select part, or in general as replacement for database fields, the result is blank.</p>
  
  <p>Here is the DDS definition and the program:</p>
  
  <blockquote>
    <p>TESTPF</p>
  </blockquote>
  
  <pre><code>A**************************************************************************
  A*
  A*-------------------------------------------------------------------------
  A*
  A          R TESTPFR
  A
  A            FLD01          2S 0
  A            FLD02         20A
  A
  A**************************************************************************
  </code></pre>
  
  <p>I have already filled this file with some dummy data. Here is what's inside:</p>
  
  <blockquote>
    <p>runqry () qtemp/testpf</p>
  </blockquote>
  
  <pre><code>       FLD01  FLD02   
  000001    1   Text 01 
  000002    2   Text 02 
  000003    3   Text 03 
  000004    4   Text 04 
  000005    5   Text 05 
  000006    6   Text 06 
  000007    7   Text 07 
  000008    8   Text 08 
  000009    9   Text 09 
  000010   10   Text 10 
  </code></pre>
  
  <p>And this is the program:</p>
  
  <blockquote>
    <p>TST001I</p>
  </blockquote>
  
  <pre><code> D**********************************************************************************************
   D*  Standalone Fields
   D*---------------------------------------------------------------------------------------------
   D stm             s            500a   inz(*blanks)
   D fieldName01     s             10a   inz(*blanks)
   D fieldName02     s             10a   inz(*blanks)
   D fieldName03     s              2a   inz(*blanks)
   D text            s             20a   inz(*blanks)
   D
   C**********************************************************************************************
   C*  M A I N   P R O G R A M M
   C**********************************************************************************************
  
     stm = 'SELECT fld02 FROM testpf WHERE fld01 = 1';
     exec sql prepare s1 from :stm;
     exec sql declare c1 cursor for s1;
     exec sql open c1;
     exec sql fetch c1 into :text;
     exec sql close c1;
     dsply text;   // Prints 'Text 01'
     text = *blanks;
  
     stm = 'SELECT fld02 FROM testpf WHERE fld01 = ?';
     exec sql prepare s2 from :stm;
     exec sql declare c2 cursor for s2;
     fieldName03 = '2';
     exec sql open c2 using :fieldName03;
     exec sql fetch c2 into :text;
     exec sql close c2;
     dsply text;   // Prints 'Text 02'
     text = *blanks;
  
     stm = 'SELECT ? FROM testpf WHERE fld01 = 3';
     exec sql prepare s3 from :stm;
     exec sql declare c3 cursor for s3;
     fieldName01 = 'FLD02';
     exec sql open c3 using :fieldName01;
     exec sql fetch c3 into :text;
     exec sql close c3;
     dsply text;   // Prints ' '
     text = *blanks;
  
     stm = 'SELECT ? FROM testpf WHERE ? = ?';
     exec sql prepare s4 from :stm;
     exec sql declare c4 cursor for s4;
     fieldName01 = 'FLD02';
     fieldName02 = 'FLD01';
     fieldName03 = '4';
     exec sql open c4 using :fieldName01, :fieldName02, :fieldName03;
     exec sql fetch c4 into :text;
     exec sql close c4;
     dsply text;   // Prints ' '
     text = *blanks;
  
     *inlr = *on;
   C********************************************************************************************** 
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>DSPLY  Text 01
  DSPLY  Text 02
  DSPLY         
  DSPLY         
  DSPLY         
  </code></pre>
  
  <p>May someone help me and explain why this is the case?</p>