Embedded SQL Method

Embedded SQL for C and SQL Server

Embedded SQL for C and SQL Server

Embedded SQL Method

Embedded SQL for C (ESQL/C) programs require preprocessing by a precompiler. The ESQL/C precompiler converts Embedded SQL statements in the program into function calls that can be accepted by a C compiler. The C compiler can then compile the resulting source code into an executable program.

For example, the following Embedded SQL code does the same task as the DB-Library example in Call-level Method:

main()
{
   EXEC SQL BEGIN DECLARE SECTION;
   char first_name[50];
   char last_name[] = "White";
   EXEC SQL END DECLARE SECTION;

   EXEC SQL CONNECT TO my_server.pubs
      USER my_login.my_password;
   EXEC SQL SELECT au_fname INTO :first_name
        from authors where au_lname = :last_name;
   return (0);
}

Note that each Embedded SQL statement starts with the introductory expression EXEC SQL. This expression tells the precompiler that the code entered between EXEC SQL and the semicolon (;) contains Embedded SQL statements.

The ESQL/C approach, using programming statements similar to Transact-SQL, is more concise than the call-level method approach and is tightly coupled to the existing database structure. Because SQL statements are directly included in the C source code, ESQL/C programs are usually special-purpose applications. ESQL/C is well-suited for environments where the C programmer is also in control of the database structure. However, ESQL/C is less flexible in environments where the database structure is changing or is not predictable. Generally, ESQL/C is used for porting your existing Embedded SQL application code to SQL Server with minimum modifications.