# Reading files (AS/400 IBMi)

You can read the contents of other files (physical and logical) directly in the transformation to retrieve external data. To do so, you would use two steps, declaration and use.

## FILE - declaration

You can declare a file in the declaration window (**3**).

The syntax for declaring a file is as follows:

```
FILE alias LIKE library/File   
```

The alias is used to reference the file buffer and its fields.

When you exit the declaration window, a new buffer named **F.alias** is added to the list (**1**).

For example, if you enter:

```
/* Declaration to access a file
FILE CUSTOMER LIKE ADVENTURE/DIMCUST
```

The F.CUSTOMER buffer is added. It contains the following fields:

* F.CUSTOMER.@EOF@
* F.CUSTOMER.CUSTKEY
* all other files’ fields

![](https://help.qlik.com/talend/en-US/change-data-capture-reference-guide/Resources/images/tcdc-f-customer-buffer.png)

The fact that the ADVENTURE/DIMCUST file name is entered against the LIKE clause does not mean that it is the file read, but only that the file read will have the same structure.

Each time a file is read, you would specify the file name as a constant or in a variable. In this way, the name of the file (library/file and possibly member) may depend on the contents of a field.

The Talend Change Data Capture engine for AS/400 IBMi handles file opening for up to 1000 files.

You can check in the field list that a column shows which are the fields that comprise the reading key and in which order.

If the physical does not have a key, you can read a logic that does have one.

## Read instructions

There are numerous read instructions so as to cover all the capabilities of IMBi.

Information noteNote: If the record is not found or the end of file is reached, the F.alias buffer is cleared and the mailto:F.alias.@EOF@ F.alias.@EOF@field is updated to 1. Otherwise, the F.alias.@EOF@ contains the value 0.

## READfirst – Read first record

```
READfirst b.Field1/Constant ON alias 
```

This reads the first record of the file in the F.alias buffer.

Example:

```
/* Declaration to access a file
FILE CATEG LIKE RAFALE/CATEG   
 
/* Reads the first record of the RAFALE/CATEG file
READfirst ‘RAFALE/CATEG’ ON CATEG
/* Copies the contents of the DESCRIPTION field in the CATEG file to the O.CATEGDESC field
O.CATEGDESC = F.CATEG.DESCRIPTION
```

This reads the first record in the RAFALE/CATEG file, retrieves the content of the DESCRIPTION field in this record, and enters it in the CATEGDESC field in the output buffer.

## READlast – Read last record

```
READlast b.Field1/Constant ON alias
```

This reads the last record of the file in the F.alias buffer.

Example:

```
/* Declaration to access a file
FILE CATEG LIKE RAFALE/CATEG   
 
/* Reads the last record of the RAFALE/CATEG file
READlast ‘RAFALE/CATEG’ ON CATEG
/* Copies the contents of the DESCRIPTION field in the CATEG file to the O.CATEGDESC field
O.CATEGDESC = F.CATEG.DESCRIPTION
```

This reads the last record in the RAFALE/CATEG file, retrieves the content of the DESCRIPTION field in this record, and enters it in the CATEGDESC field in the output buffer.

## READnext – read next record

```
READnext b.Field1/Constant ON alias
```

This reads the record following the current record of the file in the F.alias buffer.

Example:

```
/* Declaration to access a file
FILE CATEG LIKE RAFALE/CATEG   
 
/* Reads the next record of the RAFALE/CATEG file
READnext ‘RAFALE/CATEG’ ON CATEG
/* Copies the contents of the DESCRIPTION field in the CATEG file to the O.CATEGDESC field
O.CATEGDESC = F.CATEG.DESCRIPTION
```

This reads the next record in the RAFALE/CATEG file, retrieves the content of the DESCRIPTION field in this record, and enters it in the CATEGDESC field in the output buffer.

## READprevious – Read previous record

```
READprevious b.Field1/Constant ON alias
```

This reads the record preceding the current record of the file in the F.alias buffer.

Example:

```
/* Declaration to access a file
FILE CATEG LIKE RAFALE/CATEG   
 
/* Reads the previous record of the RAFALE/CATEG file
READprevious ‘RAFALE/CATEG’ ON CATEG
/* Copies the contents of the DESCRIPTION field in the CATEG file to the O.CATEGDESC field
O.CATEGDESC = F.CATEG.DESCRIPTION
```

This reads the previous record in the RAFALE/CATEG file, retrieves the content of the DESCRIPTION field in this record, and enters it in the CATEGDESC field in the output buffer.

## READkey – Read by key

```
READkey b.Field1/Constant ON alias WITHKEY EQ    TO b.Field2/Constant
                                                  GE       
                                                  GT       
                                                  LE       
                                                  LT       
                                                  NEXTEQ   
                                                  NEWUNIQ  
                                                  PREVEQ   
                                                  PREVUNIQ                                        
```

This reads the record matching the key criterion for the file in the F.alias buffer.

The following key criteria can be used:

| Key | Description |
| --- | --- |
| EQ | Searches for the record having the same value as the key |
| GE | Searches for the record having a value greater than or the same as the<br>key |
| GT | Searches for the record having a value greater than the key |
| LE | Searches for the record having a value less than or the same as the<br>key |
| LT | Searches for the record having a value less than the key |
| NEXTEQ | Searches for the next record having the same value as the key |
| NEXTUNIQ | Searches for the next record having the unique key value |
| PREVEQ | Searches for the previous record having the same value as the key |
| PREVUNIQ | Searches for the previous record having the unique key value |

The key may be partial. If, for example, the key for the AS/400 IBMi file you want to read is KEYFIELD1, KEYFIELD2, and KEYFIELD3, you can use only KEYFIELD1, or KEYFIELD1 followed by the first three characters in KEYFIELD2.

If the key of the AS/400 IBMi file is composed of several fields, and you want to search based on the entire key, you can, for example, define a full-length field with sub-descriptions using the BASED clause for the DCL instruction.

Example:

```
FILE ORDER LIKE OMBIL_SRC/ORDERL01   
 
/* The key of the ORDERL01 file is STE(2 alpha), CODCLI(10 Num), NUMCDE(9 PACKED)
DCL W.CDE_KEY CHAR 17
  DCL W.CDE_KEY.STE CHAR 2 BASED CDE_KEY POS 1
  DCL W.CDE_KE.CUSTCODE ZONED 10,0 BASED CDE_KEY POS 3
  DCL W.CDE_KEY.NUMCDE PACKED 9,0 BASED CDE_KEY POS 13
 
/* Loads the read key
W.CDE_KEY.STE = S.JRN_MBR
W.CDE_KEY.CODECLI = I.CODECLI
W.CDE_KEY.NUMCDE = I.NUMCDE
 
/* Reads the record with key in the OMBIL_PRD/ORDERL01 file
READkey ‘OMBIL_PRD/ORDERL01’ ON CATEG WITHKEY EQ TO W.CDE_KEY
O.NAME = F.ORDER.NAME
```

This reads the record in the OMBIL\_PRD/ORDERL01 file (note that the name of the library is not the same as in the FILE declaration) with the company's composite key.