This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
||||||||||||||||||||||||||||
|
4D v20 R7
Overview of SQL Commands
|
4D Type | SQL Type |
C_STRING | SQL_C_CHAR |
C_TEXT | SQL_C_CHAR |
C_REAL | SQL_C_DOUBLE |
C_DATE | SQL_C_TYPE_DATE |
C_TIME | SQL_C_TYPE_TIME |
C_BOOLEAN | SQL_C_BIT |
C_INTEGER | SQL_C_SHORT |
C_LONGINT | SQL_C_SLONG |
C_BLOB | SQL_C_BINARY |
C_PICTURE | SQL_C_BINARY |
C_GRAPH | SQL_C_BINARY |
C_OBJECT | SQL_C_BINARY |
4D provides two ways for inserting 4D expressions (variables, arrays, fields, pointers, valid expressions) into SQL requests: direct association and the setting of parameters using SQL SET PARAMETER.
Direct association can be carried out in two ways:
SQL EXECUTE("INSERT INTO emp (empnum,ename) VALUES (<<vEmpnum>>,<<vEname>>)")
SQL EXECUTE("SELECT age FROM People WHERE name= :vName")
Note: In compiled mode, you can use references to local variables (beginning with the $ symbol) under certain conditions only (see below).
In these examples, the current values of the 4D vEmpnum, vEname and vName variables will replace the parameters when the request is executed. This solution also works with 4D fields and arrays.
This easy-to-use syntax nevertheless has the drawback of not being compliant with the SQL standard and of not allowing the use of output parameters. To remedy this, you can use the SQL SET PARAMETER command. This command can be used to set each 4D object to be integrated into a request as well as its mode of use (input, output or both). The syntax produced is thus standard. For more information, please refer to the description of the SQL SET PARAMETER command.
1. This example executes an SQL query that directly uses the associated 4D arrays:
ARRAY TEXT(MyTextArray;10)
ARRAY LONGINT(MyLongintArray;10)
For(vCounter;1;Size of array(MyTextArray))
MyTextArray{vCounter}:="Text"+String(vCounter)
MyLongintArray{vCounter}:=vCounter
End for
SQL LOGIN("mysql";"root";"")
SQLStmt:="insert into app_testTable (alpha_field, longint_field) VALUES (<<MyTextArray>>, <<MyLongintArray>>)"
SQL EXECUTE(SQLStmt)
2. This example can be used to execute an SQL query that directly uses the associated 4D fields:
ALL RECORDS([Table 2])
SQL LOGIN("mysql";"root";"")
SQLStmt:="insert into app_testTable (alpha_field, longint_field) VALUES (<<[Table 2]Field1>"+">,<<[Table 2]Field2>>)"
SQL EXECUTE(SQLStmt)
3. This example lets you execute an SQL query by directly passing a variable via a dereferenced pointer:
C_LONGINT($vLong)
C_POINTER($vPointer)
$vLong:=1
$vPointer:=->$vLong
SQL LOGIN("mysql";"root";"")
SQLStmt:="SELECT Col1 FROM TEST WHERE Col1=:$vPointer"
SQL EXECUTE(SQLStmt)
In compiled mode, you can use local variable references (beginning with the $ character) in SQL statements under certain conditions:
SQL EXECUTE("select * from t1 into :$myvar") // works in compiled mode
C_TEXT(tRequest)
tRequest:="select * from t1 into :$myvar"
SQL EXECUTE(tRequest) // error in compiled mode
Retrieving values in the 4D language that result from SQL queries is carried out in two ways:
It is possible to place the results of an SQL query directly in an array type list box. This offers a rapid means for viewing the results of SQL queries. Only queries of the SELECT type can be used. This mechanism cannot be used with an external SQL database.
It works according to the following principles:
Example
We want to retrieve all the fields of the PEOPLE table and put their contents into the list box having the variable name vlistbox. In the object method of a button (for example), simply write:
Begin SQL
SELECT * FROM PEOPLE INTO <<vlistbox>>
End SQL
_o_EXPORT ODBC
_o_IMPORT ODBC
Blog: Read 4D object fields with SQL Engine
Product: 4D
Theme: SQL
Modified: 4D v17 R5
4D Language Reference ( 4D v20 R7)