crchin
crchin

Reputation: 9683

Spaces filled when value is null

I am doing a flat file writing on cobol. but when the variable is null , it will not fill with spaces. Please help.

Here is some of the part of flat file structure declaration.

01 Fs-IL494-REC.
   03 FX-IL494-TRXN-1-10                     PIC X(410) VALUE SPACES.

01 Wx-TRXN-INFO-BUF                PIC X(410) VALUE SPACES.
01 Ws-TRXN-INFO REDEFINES Wx-TRXN-INFO-BUF OCCURS 10 TIMES.
03 Wx-TRXN-DT                   PIC X(8).
03 Wx-TRXN-CDE                  PIC X(3).
03 W9-TRXN-AMT                  PIC S9(13)V9(2)
                                    SIGN LEADING SEPARATE.
03 Wx-TRXN-DESC                 PIC X(14).

Doing some query to retrive the records from database.

INITIALIZE Wx-TRXN-INFO-BUF.
INITIALIZE FX-IL494-TRXN-1-10.

ADD 1 TO W9-ARR-CNT
MOVE F9-IRC-TXN-DT TO Wx-TRXN-DT(W9-ARR-CNT)
MOVE F9-IRC-TXN-CDE TO Wx-TRXN-CDE(W9-ARR-CNT)
MOVE F9-IRC-TXN-AMT TO W9-TRXN-AMT(W9-ARR-CNT)
MOVE FX-IRC-TXN-DESC TO Wx-TRXN-DESC(W9-ARR-CNT)

MOVE Wx-TRXN-INFO-BUF TO FX-IL494-TRXN-1-10

Upvotes: 1

Views: 4215

Answers (2)

Joe Zitzelberger
Joe Zitzelberger

Reputation: 4263

Initialize is tied to the value clause, and it's behavior can be quirky and annoying.

Far better to simple "Move spaces to my-record" before you do your read...

Upvotes: 0

NealB
NealB

Reputation: 16928

My assumptions:

  • This is SQL (DB/2 probably)
  • You are doing a SELECT INTO :host-variable
  • The host-variable is the thing you expect to be set to SPACES when the corresponding database table column contains a NULL value.

If the above is correct, the problem is that when a NULL value is selected off of the database, the corresponding host-variable is not updated. It will retain whatever value it had before the query was executed.

You may need to add a null value indicator to your query. When the null value indicator is set to a value less than zero (often -1) the corresponding host-variable is unchanged. This is the pattern you should be using:

EXEC SQL
   SELECT column-name-1,
          column-name-2
   INTO :host-var-1 :null-ind-1,
        :host-var-2 :null-ind-2
   FROM some-table
   WHERE bla bla bla...
END-EXEC

The "extra" variables null-ind-1 and null-ind-2 should be declared as S9(4) USAGE COMP. Notice that comma is "missing" between the host variable and the null indicator. The null indicator variables will be assigned a value less than zero when the corresponding column-name-1 or column-name-2 contain NULL values. Should that be the case, then host-var-1 and host-var-2 will remain unchanged (i.e. retain whatever values they had before the EXEC SQL.

In your code after the EXEC SQL you should be doing something like:

IF null-ind-1 < ZERO
   MOVE SPACES TO host-var-1
END-IF
IF null-ind-2 < ZERO
   MOVE SPACES to host-var-2
END-IF

I have also seen programmers simply initialize all host variables before doing the EXEC SQL to whatever they would want in event of a NULL column value and not bother with null indicators on the query (not hard to figure out why that would work once you know that DB2 will not update the host variable in event of a NULL value)..

Upvotes: 3

Related Questions