4D v16Accessing the 4D SQL Engine |
||
|
4D v16
Accessing the 4D SQL Engine
Accessing the 4D SQL Engine
The 4D built-in SQL engine can be called in three different ways:
QUERY BY SQL([OFFICES];"SALES > 100")
It is possible to reference any type of valid 4D expression (variable, field, array, expression...) within WHERE and INTO clauses of SQL expressions. To indicate a 4D reference, you can use either of the following notations:
Examples: C_TEXT(vName) or: C_TEXT(vName) Note: The use of brackets [] is required when you work with interprocess variables (for example, <<[<>myvar]>> or :[<>myvar]). In compiled mode, you can use local variable references (beginning with the $ character) in SQL statements under certain conditions:
The data retrieval in a SELECT statement will be managed either inside Begin SQL/End SQL tags using the INTO clause of the SELECT command or by the "SQL" language commands.
The main difference between these two ways of getting data from SQL (Begin SQL/End SQL tags and SQL commands) is that in the first case all the information is sent back to 4D in one step, while in the second case the records must be loaded explicitly using SQL LOAD RECORD. For example, supposing that in the PEOPLE table there are 100 records:
Here we have to loop 10 times to retrieve all 100 records. If we want to load all the records in one step we should use:
In this situation, after the execution of the SELECT statement, the aBirthYear array size becomes 100 and its elements are filled with all the birth years from all 100 records.
Here we have to loop 10 times to retrieve all the 100 records. Every step will create 10 records in the [MYTABLE] table and store each retrieved Birth_Year value from the PEOPLE table in the Birth_Year field.
In this case, during the execution of the SELECT statement, there will be 100 records created in the [MYTABLE] table and each Birth_Year field will contain the corresponding data from the PEOPLE table, Birth_Year column. 4D includes a specific automatic functioning (LISTBOX keyword) that can be used for placing data from SELECT queries into a listbox. For more information, please refer to the Design Reference manual. For optimization purposes, it is preferable to use 4D expressions rather than SQL functions in queries. 4D expressions will be calculated once before the execution of the query whereas SQL functions are evaluated for each record found. For example, with the following statement: SQL EXECUTE("SELECT FullName FROM PEOPLE WHERE FullName=<<vLastName+vFirstName>>") ... the vLastName+vFirstName expression is calculated once, before query execution. With the following statement: SQL EXECUTE("SELECT FullName FROM PEOPLE WHERE FullName=CONCAT(<<vLastName>>,<<vFirstName>>)") ... the CONCAT(<<vLastName>>,<<vFirstName>>) function is called for each record of the table; in other words, the expression is evaluated for each record.
See also
|
PROPERTIES
Product: 4D
HISTORY
ARTICLE USAGE
4D SQL Reference ( 4D v16) |