READ

Syntax

READ {filenum_lit|_var|_col} INTO {any_var:length_int_lit}...[STATUS=status_num_var]

Description

Reads the next record of a file into the specified variables.

Text and binary data are parsed according to the following criteria:

  • Text data is any string of characters. The length of the variable name indicates how many characters to place in the variable.

    After text is transferred, trailing blanks in the variable are omitted.

  • If the field was written as a date variable, then it can be read into a date variable or text variable.

    When reading a date into a date variable, it must be in the format specified by the SQR_DB_DATE_FORMAT setting, one of the database-dependent formats as listed in the DATE Column Formats table, or the database-independent format 'SYYYYMMDD[HH24[MI[SS[NNNNNN]'.

  • Binary numbers can be 1, 2, or 4 bytes in length.

    They must be read into numeric variables. Note that the bytes making up the binary number must be in the standard sequence expected by your operating system.

  • When the program is reading binary data, the file must be opened with the FIXED or FIXED-NOLF qualifier.

  • Only the integer portion of the number is represented with binary numbers.

    To maintain the decimal portion of the number, convert the number to a string variable.

  • If you use binary numbers, the file is not portable across platforms.

    Different hardware represents binary numbers differently.

The total length indicated for the variables must be less than or equal to the length of the record being read.

If no more records exist to read, the #end-file reserved variable is set to 1; otherwise, it is set to 0 (zero). Your program should check this variable after each READ command.

If STATUS is specified, SQR returns 0 if the read is successful; otherwise, it returns the value of errno, which is system-dependent.

Parameters

Parameter Description

filenum_lit|_var | _col

Specifies the number assigned in the OPEN command to the file to be read.

any_var :length_int_lit

Specifies one or more variables into which data from the record that is read are to be put. length_int_lit specifies the length of each field of data.

STATUS

Specifies an optional variable into which a read status is returned.

Example

The following example shows several READ commands:

read  1  into  $name:30  $addr:30  $city:20  $state:2  $zip:5
read  3  into  $type:2  #amount:2  #rate:1  $code:5  $date:11
read  #j into  #sequence:2  $name:20  $title:15

The following example shows a READ command that reads two dates. One is loaded into a date variable; the other is loaded into a string variable, which is then converted to a date using the strtodate function.

.
.
.
declare-variable
 date $date1 $date2
 text $text
end-declare
.
.
.
read 4 into $date1:18 $text1:18
let $date2 = strtodate($text1,'SYYYYMMDDHHMISSNNN')
         or
let $date2 = strtodate($text1)

The following example shows a READ command with an INSERT loop:

begin-sql
 begin transaction
end-sql

while  1    ! Infinite loop, exited by BREAK, below.
 read  10  into  $company:40  $parent:30  $location:50
 if #end-file
  break   ! End of file reached.
 end-if
 begin-sql
  insert into comps (name, parent, location)
     values ($company, $parent, $location)
 end-sql
 add 1 to #inserts
 if #inserts >= 100
   begin-sql
    end transaction;
    begin transaction
   end-sql
 move 0 to #inserts
 end-if
end-while

begin-sql
 end transaction
end-sql

See commands for information about filesOPEN, CLOSE, WRITE