KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: EXECUTE IMMEDIATE Supports Embedded Variables
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: September 24, 2010

The SQL command EXECUTE IMMEDIATE can be used to execute dynamically defined SQL statements as shown in this Tech Tip.

It is important to note that 4D variable references can be embedded in these SQL statements. For an example take a look at this code snippet:

$statement_t:="SYNCHRONIZE LOCAL TABLE "+$tableDef_t
$statement_t:=$statement_t+" WITH "
$statement_t:=$statement_t+" REMOTE TABLE "+$tableDef_t
$statement_t:=$statement_t+" FOR REMOTE STAMP :remoteStamp_l,"
$statement_t:=$statement_t+" LOCAL STAMP :localStamp_l"
$statement_t:=$statement_t+" LOCAL OVER REMOTE"
$statement_t:=$statement_t+" LATEST REMOTE STAMP :latestRemoteStamp_l,"
$statement_t:=$statement_t+" LATEST LOCAL STAMP :latestLocalStamp_l;"

Begin SQL
   EXECUTE IMMEDIATE :$statement_t;
End SQL


Because the 4D SQL engine evaluates the statement on the fly, the variable remoteStamp_l, localStamp_l, latestRemoteStamp_l, and latestLocalStamp_l can be embedded in the text variable. Take note: it is not the value in the variables being used, but the variables themselves!

Use caution however: errors with this kind of statement can only be caught at runtime. The 4D compiler will be unable to check to see if the variables actually exist at compile time.