Embedding SQL statements in host programming language: disadvantages, example

SQL statements are embedded in application programs by the following process:

1) In the source program, SQL statements are mixed with statements of host programming language. This results in embedded SQL source program. After that the embedded SQL source program is sent to a pre-compiler for processing.

2) Embedded SQL statements can reference variables of host language. Values which are calculated by the program are used by SQL statements.

3) To assign NULL values to database columns, and to retrieve NULL values from the database, program variables are used.

4) To get results of SQL queries, program variables are used by the embedded SQL statements.

5) Special embedded SQL statements are used for providing row by row processing of query results.

Disadvantages of embedded SQL:

1) Embedded SQL is a mixture of SQL with the host language. It is a difficult task to understand the source program.

2) Special embedded SQL statements are used in embedded SQL which are not used in interactive SQL.

In embedded SQL, the host language strategy of programming is used along with the SQL statements. Here’s an embedded SQL program in C language to delete an employee from employees table whose sales is less than 200000:

exec sql include sqlca;
exec sql declare employees table
(empl_num integer not null,
empl_name varchar(30) not null,
age integer,
title varchar(30),
hire_date date not null,
sales money not null);
printf(“Delete employees with lower sales\n”);
exec sql delete from employees where sales < 200000.00;
printf(“Delete Completed\n”);

Here, exec sql is an introducer which informs that it is a SQL statement and semicolon acts as the terminator which informs that the SQL statements have ended.

Leave a Reply