4D v12.4Overview of SQL Commands |
||||||||||||||||||||||||||
|
4D v12.4
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 |
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 cannot use references to local variables (beginning with the $ symbol).
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:
Product: 4D
Theme: SQL
Accessing the 4D SQL Engine
EXPORT ODBC
IMPORT ODBC