SelectByKey method: Record class
Syntax
SelectByKey([UseChangedBuffers])
Description
The SelectByKey method uses the key field names of the record and their values to build and execute a Select SQL statement. The field values are then fetched from the database SQL table into the record object executing the method, and the Select statement is closed.
Note:
You can't use this method in dynamic views.
If you don’t specify all the key fields for a record, those you exclude are added to the Where clause with the condition equal to a blank value. If not all keys are set and more than one row is retrieved, you won't receive an error and SelectByKey won’t fetch any data.
SelectByKey returns a single row of data. To fetch more than one row, use the SQL object.
For every record read by the SelectByKey method, if the set language is not the base language and the record has related language records, the SelectByKey method tries to do related language processing.
You can also select into a record using SQLExec and meta-SQL. For example, if you need to use SelectByKey with an effective date, you can use the meta-SQL %SelectByKeyEffDt in a SQLExec statement. In the following example, &RECOBJ is the name of a record created using the CreateRecord function.
SQLExec("%SelectByKeyEffDt(:1, :2)", &RECOBJ, %Date, &RECOBJ);
Parameters
| Parameter | Description |
|---|---|
|
UseChangedBuffers |
Specify whether to retrieve the data into the changed or original buffers. The system keeps track of the original and changed values. This parameter takes a Boolean value: true, retrieve into the changed buffers, False, use the original buffers. The default value is true. That is, to retrieve the data into the changed buffers. To use this parameter with the Save method, it is important that data be loaded into the original buffers in order for the save logic to work. To do that specify a value of false for this parameter. |
Returns
The result is true on successful completion, false if the record was not found. Any other conditions cause termination.
If false is returned, the system resets the record values to their default values for scrolls that are not derived. If you do a manual insert afterwards (using the Insert method) you may insert a record with null keys.
Example
Suppose that KEYF1 and KEYF2 are the two key fields of record definition MYRECORD. The following code reads the database record that has KEYF1 equal to "A" and KEYF2 equal to "X" into the &REC record object:
Local record &REC;
&REC = CreateRecord(RECORD.MYRECORD);
&REC.KEYF1.Value = "A";
&REC.KEYF2.Value = "X";
&REC.SelectByKey();
The following example verifies if the method completes successfully before using the Insert method.
if not &Rec.SelectByKey() then
/* Must duplicate this key field population before the Insert - else can get⇒
'blank' row */
&Rec.KEY1.Value = ...;
&Rec.KEY2.Value = ....;
....
&Rec.Insert();
end-if;