ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQL Rpgle - Fetch into Clob file - avoid null

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQL Rpgle - Fetch into Clob file - avoid null

    Check below example,

    Exec Sql Declare cursor c1 for
    Select fld1, fld2 from tempfile;

    Exec Sql Open c1

    Dow Sqlcod = 0;
    Exec Sql Fetch c1 into :Outfile; // clob file
    Enddo;

    Problem when there are no records, the file is still created with null. Please suggest how better to handle this stop creating the file when there are no records.



  • #2
    There isn't anything in the code you provided that would create a file.

    Comment


    • #3
      I constructed a similar program to try this myself and I found that when the cursor has one or more rows then the fetch statement creates the file, but when the cursor has zero rows it doesn't create the file. A lot seems to depend on how the Outfile variable is declared and initialized, though. Could there be some code earlier in your program which is actually creating the file?

      This is the code I tested with:

      Code:
      dcl-s ifsFile sqltype(CLOB_FILE);
      dcl-s fileName varchar(256);
      
      fileName = '/home/XXXX/r386.txt';
      ifsFile_NAME = fileName;
      ifsFile_NL = %len(fileName);
      ifsFile_FO = SQFOVR; // overwrite
      // ifsFile_FO = SQFAPP; // append
      
      exec sql
      declare C1 cursor for
      select F1, F2 from LIB1/TAB1;
      
      exec sql
      open C1;
      
      dow SQLCOD = 0;
      exec sql
      fetch C1 into :ifsFile;
      enddo;
      
      exec sql
      close C1;

      Do you set Outfile_FO to any specific value?

      I think only the first column ('fld1' in your example) defined in the select statement is getting written to the file, though. Is that the intention?

      Comment

      Working...
      X