#sql directive

Use the #sql directive to customize implicit SQL code, or to write entirely new code.

For more about implicit versus embedded SQL code, see SQL data access.

Typically, you introduce your embedded SQL code by using the with keyword. The execute statement is an exception, as it does not use with. Next, use the #sql keyword and, with no intervening spaces, an open brace. End the embedded code with a closing brace, as in the following example:
get myCustomer with #sql{
  SELECT customer_name
  FROM Customer
  WHERE customer_number = :myCustomer.customerNumber };

An initial colon character indicates a host variable (a variable defined in the language hosting SQL, in this case, EGL), for example, :myCustomer.customerNumber.

You can convert implicit SQL code to embedded code and then modify it; for more information, see the EGL Programmer's Guide.

Embedded SQL statements take priority over implicit ones. In the following negative example, there are three conflicting key fields implied or called for:
get myCustomer withKeys myCustomer.customerName with #sql{
  SELECT customer_name
  FROM Customer
  WHERE customer_number = :myOrders.customerNumber };
The first key field is that specified in the record prototype, CustomerRecord (myCustomer.customerNumber). The withKeys clause overrides this default key with myCustomer.customerName in the implicit SQL code that EGL generates when there is not embedded code as well. That embedded code overrides the implicit code and uses myOrders.CustomerNumber as the actual key to read from the database.

Feedback