Export (0) Print
Expand All

Embedded SQL Method

SQL Server 2000

  New Information - SQL Server 2000 SP3.

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:

   char first_name[50];
   char last_name[] = "White";

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

Security Note  The preceding example uses the $integrated keyword to enable Windows Authentication. When possible, use this authentication method. If Windows Authentication is not available, prompt users to enter their credentials at run time. Avoid storing credentials in a file. If you must persist credentials, you should encrypt them with the Win32 cryptoAPI.

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.

© 2014 Microsoft