Embedded SQL
Embedded SQL
There are some special embedded SQL statements that are used to retrieve the data into the program.
There is a special SQL pre-compiler that accepts the combined source code with other programming
tools and converts them into an executable program.
1. The programming language in which the SQL statements are embedded is called the
host language. The SQL statements and host language statements make the source
program which is fed to a SQL pre-compiler for processing the SQL statements.
2. The host programming languages variables can be referenced in the embedded SQL
statements, which allows values calculated by the programs to be used by SQL
statements.
3. There are some special program variables that are used to assign null values to
database columns. These program variables support the retrieval of null values from
the database.
The SQL standard defines embeddings of SQL in a variety of programming languages, such as C, C+
+, Cobol, Pascal, Java, PL/I, and Fortran.
A language in which SQL queries are embedded is referred to as a host language, and the SQL
structures permitted in the host language constitute embedded SQL.
Programs written in the host language can use the embedded SQL syntax to access and update data
stored in a database.
The pre-processor replaces embedded SQL requests with host-language declarations and procedure
calls that allow runtime execution of the database accesses.
To identify embedded SQL requests to the pre-processor, we use the EXEC SQL statement; it has the
form:
Before executing any SQL statements, the program must first connect to the database. Variables of the
host language can be used within embedded SQL statements, but they must be preceded by a colon (:)
to distinguish them from SQL variables.
To iterate over the results of an embedded SQL query, we must declare a cursor variable, which can
then be opened, and fetch commands issued in a host language loop to fetch consecutive rows of the
query result.
Attributes of a row can be fetched into host language variables. Database updates can also be
performed using a cursor on a relation to iterate through the rows of the relation, optionally using a
where clause to iterate through only selected rows.
Embedded SQL commands can be used to update the current row where the cursor is pointing. The
exact syntax for embedded SQL requests depends on the language in which SQL is embedded.
In JDBC, SQL statements are interpreted at runtime (even if they are created using the prepared
statement feature). When embedded SQL is used, there is a potential for catching some SQL-related
errors (including data-type errors) at the time of pre-processing.
SQL queries in embedded SQL programs are also easier to comprehend than in programs using
dynamic SQL. However, there are also some disadvantages to embedded SQL.
The pre-processor creates new host language code, which may complicate debugging of the program.
The constructs used by the pre-processor to identify SQL statements may clash syntactically with the
host language syntax introduced in subsequent versions of the host language.
As a result, most current systems use dynamic SQL, rather than embedded SQL. One exception is the
Microsoft Language Integrated Query (LINQ) facility, which extends the host language to include
support for queries instead of using a pre-processor to translate embedded SQL queries into the host
language.
Embedded SQL gives us the freedom to use databases as and when required. Once the application we
develop goes into production mode several things need to be taken care of.
We need to take care of a thousand things out of which one major aspect is the problem of
authorization and fetching and feeding of data into/from the database.
With the help of the embedding of queries, we can easily use the database without creating any bulky
code. With the embedded SQL, we can create API’s which can easily fetch and feed data as and when
required.
For using embedded SQL, we need some tools in each high-level language. In some cases, we have
inbuilt libraries which provide us with the basic building block.
While in some cases we need to import or use some packages to perform the desired tasks.
For example, in Java, we need a connection class. We first create a connection by using the
connection class and further we open the connection bypassing the required parameters to connect
with the database.
Some of the advantages of using SQL embedded in high-level languages are as follows:
EMBEDDED DATABASES
Both JDBC and ODBC assume that a server is running on the database system hosting the database.
Some applications use a database that exists entirely within the application. Such applications
maintain the database only for internal use and offer no accessibility to the database except through
the application itself. In such cases, one may use an embedded database and use one of several
packages that implement an SQL database accessible from within a programming language. Popular
choices include Java DB, SQLite, HSQLBD, and ˝2. There is also an embedded version of MySQL.
Embedded database systems lack many of the features of full server-based database systems, but they
offer advantages for applications that can benefit from the database abstractions but do not need to
support very large databases or large-scale transaction processing. Do not confuse embedded
databases with embedded SQL; the latter is a means of connecting to a database running on a server.