Embedded SQL Method

Embedded SQL programs require preprocessing by a precompiler. The Embedded SQL 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 tasks as the DB-Library example in the "Call-level Method" section earlier in this chapter:


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 that is entered between EXEC SQL and the semicolon (;) contains Embedded SQL statements.

The Embedded SQL 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 the SQL statements are directly included in the C source code, Embedded SQL programs are usually special-purpose applications. Embedded SQL is well suited for environments where the C programmer is also in control of the database structure. However, Embedded SQL is less flexible in environments where the database structure is changing or is not predictable.