KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Understanding the ways to employ SQL in 4D with using an external data file
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: November 17, 2015

It is important to understand that SQL commands in 4D can be coded in three different ways with using an external data file.

    1. Inside a Begin SQL - End SQL block
    2. Using SQL EXECUTE, outside of a Begin SQL - End SQL block
    3. Using EXECUTE IMMEDIATE, inside a Begin SQL - End SQL block

NOTE: All commands under the 4D Language SQL theme are not SQL commands, rather, they are 4D commands that facilitate the use of SQL in 4D.

As an example, consider the Begin SQL - End SQL code blocl below...

Begin SQL
    USE DATABASE DATAFILE :Path_T;
    SQLStmt_T:="SELECT COUNT(1) FROM Tapes WHERE Artist LIKE ?" ///Syntax Error
    SQL SET PARAMETER(Artist;SQL Param In)
    SQL EXECUTE(SQLStmt;$ArtistNo)
    SQL LOAD RECORD(-1)
    SQL CANCEL LOAD
End SQL


Using method #1, the code would look like this:

Begin SQL
    USE DATABASE DATAFILE :Path_T;
    SELECT COUNT(1) FROM Tapes WHERE Artist LIKE :Artist;
End SQL


Using method #2, the code would look like this:

SQLStmt_T:="USE DATABASE DATAFILE :Path;SELECT COUNT(1) FROM Tapes WHERE Artist LIKE ?;"
SQL SET PARAMETER(Artist;SQL Param In)
SQL EXECUTE(SQLStmt_T;$ArtistNo)
SQL LOAD RECORD(-1)
SQL CANCEL LOAD


Using method #3, the code would look like this:

SQLStmt_T:="SELECT COUNT(1) FROM Tapes WHERE Artist LIKE :Artist;"

Begin SQL
    USE DATABASE DATAFILE :Path_T;
    EXECUTE IMMEDIATE :SQLStmt_T;
End SQL