WRITE
Syntax
WRITE {filenum_lit|_var|_col} FROM {{{txt_lit|_var|_col}|{date_var|_col}|num_col} [:len_int_lit]}|{num_lit|_var:len_int_lit}}... [STATUS=status_num_var]
Description
Writes a record to a file from data stored in variables, columns, or literals.
The file must already be opened for writing.
If length is specified, the variable is either truncated at that length or padded with spaces to that length. If length is not specified (for string variables or database columns), the current length of the variable is used.
When you are writing numeric variables, the length argument is required. Only 1-byte, 2-byte, or 4-byte binary integers are written. Floating point values are not supported directly in the WRITE command. However, you can first convert floating point numbers to strings and then write the string.
When you are writing binary data, you must open the file using the FIXED or FIXED-NOLF qualifiers. The file is not portable across platforms because binary numbers are represented differently.
When writing a date variable or column, the date is converted to a string according to the following rules:
-
For DATETIME columns and SQR DATE variables, SQR uses the format specified by the SQR_DB_DATE_FORMAT setting.
If this has not been set, SQR uses the first database-dependent format as listed in the Default Database Formats table.
-
For DATE columns, SQR uses the format specified by the SQR_DB_DATE_ONLY_FORMAT setting.
If this has not been set, SQR uses the format listed in the DATE Column Formats table.
-
For TIME columns, SQR uses the format specified by the SQR_DB_TIME_ONLY_FORMAT setting.
If this has not been set, SQR uses the format as listed in the TIME Column Formats table.
Text literals take the length of the literal.
Files opened for writing are treated as having variable-length records. If you need a fixed-length record, specify a length for each variable written to the file.
The total length of the variables and literals being written must not be greater (but can be less) than the record length specified when the file was opened. Records are not padded, but are written with the total length of all variables in the WRITE command.
If STATUS is specified, SQR returns 0 if the write 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 written. |
|
{{txt_lit|_var|_col}| {date_var|_col}|num_col} [:len_int_lit]}| {num_lit|_var:len_int_lit} |
Specifies one or more variables to be written. len_int_lit specifies the length of each field of data. |
|
STATUS |
Specifies an optional variable into which a write status is returned. |
Example
The following example shows the WRITE command:
write 5 from $name:20 $city:15 $state:2
write 17 from $company ' - ' $city ' - ' $state ' ' $zip
write #j2 from #rate:2 #amount:4 #quantity:1
move #total to $tot 99999.99 ! Convert floating point to string.
write 1 from $tot
let $date1 = datenow() ! Put the current date and time
! into DATE variable
write 3 from $date1:20
See OPEN, CLOSE, READ