Sie sind auf Seite 1von 5

Static or Embedded and Dynamic or Interactive SQL

Database languages are meant for dealing with databases. They are used only
to query tables / views, manipulate the data values using insert/update/
delete, or set of transactions can be executed one after the other to modify set
of tables/view. But all these transactions can be performed by a good database
developer or a SQL programmer who has good knowledge about database as
well as SQL. But in real time, these developer or programmers will not be using
the database. Actual use of DB is for normal user. In order to make easy for the
normal user, applications, UI or forms are created where user can enter his
values or requirement. The underlying application program will manipulate his
request. Hence user need not have any knowledge about the DB. Applications
are developed using some general purpose languages like C, C++, JAVA, etc.
These languages are used to get UIs, forms etc. They are not meant for any DB
activities. Similarly, DB languages like SQL, SQL servers etc. are meant for only
DB activities. Hence both application language and DB language are completely
different from each other. But in order to make application programs to work,
DB is compulsory; and for any DB to be used by the user application/UI is a
must. Hence both are dependent on each other too.
This gap between application programs and SQL is bridged by the use
of embedded SQL. These SQL provide the utility to use SQL inside the
application language (Host Language) like C, C++, Java etc., and make
these applications to communicate with DB. Hence when user submits
a request or enters values in the form, he gets the result what he is
requested.
Static or Embedded SQL are SQL statements in an application that do not
change at runtime and, therefore, can be hard-coded into the application.
Dynamic SQL is SQL statements that are constructed at runtime; for example,
the application may allow users to enter their own queries. The mixture of SQL
and general purpose programming languages (like C, C++, Java) is called
embedded SQL.
Dynamic SQL is a programming technique that enables you to build SQL
statements dynamically at runtime. We can create more general purpose,
flexible applications by using dynamic SQL because the full text of a SQL
statement may be unknown at compilation.
Basic differences between Embedded and Dynamic SQL:

STATIC (EMBEDDED) SQL DYNAMIC (INTERACTIVE) SQL


1 In Static SQL, how database will In Dynamic SQL, how database
be accessed is predetermined in will be accessed is determined at
the embedded SQL statement. run time.
2 It is more efficient. It is less efficient.
3 SQL statements are compiled at SQL statements are compiled at
compile time. run time.
4 Parsing, Validation, Optimization Parsing, Validation, Optimization
and Generation of application plan and Generation of application plan
are done at compile time. are done at run time.
5 It is generally used for situations It is generally used for situations
where data is distributed where data is distributed non
uniformly. uniformly.
6 It is less flexible. It is more flexible.

Embedded SQL
Embedded SQL is the one which combines the high level language with the DB
language like SQL. It allows the application languages to communicate with DB
and get requested result. The high level languages which supports embedding
SQLs within it are also known as host language. When SQL is embedded within
C or C++, then it is known as Pro*C/C++ or simply Pro*C language. Pro*C is the
most commonly used embedded SQL.
When SQL is embedded within C language, the compiler processes the
compilation in two steps. It first extracts all the SQL codes from the
program and the pre-compiler will compile the SQL code for its syntax,
correctness, execution path etc. Once pre-compilation is done, these
executable codes are embedded into the C code. Then the C compiler
will compile the code and execute the code. Thus the compilation takes
place in two steps – one for SQL and one for application language. Hence
these types of compilation require all the query, data value etc. to be known at
the compilation time itself to generate the executable code. Otherwise C or any
high level language cannot compile the code. Hence the SQL codes written are
static and these embedded SQL is also known as static SQL.
When SQL is embedded in C programming language, the C compiler will not
understand which the syntax of C is and which syntax of SQL is. It needs to be
clearly differentiated and compiler should know which is C and SQL. This is
very important as pre-compiler will first extract all the SQLs embedded in it to
compile it at DB level. Then it will be embedded in the C code which will be
compiled by the C compiler to get executable code. All the embedded SQLs are
preceded by ‘EXEC SQL’ and ends in semicolon (;). We can have these SQLs
placed anywhere in the C code, provided it is placed in the correct order-
declaration, execution and end. Let us see below how C code is differentiated
from SQL code.
Structure of Embedded SQL
Structure of embedded SQL defines step by step process of establishing a
connection with DB and executing the code in the DB within the high level
language.
Connection to DB
This is the first step while writing a query in high level languages. First
connection to the DB that we are accessing needs to be established. This can
be done using the keyword CONNECT. But it has to precede with ‘EXEC SQL’
to indicate that it is a SQL statement.

EXEC SQL CONNECT db_name;

Declaration Section
Once connection is established with DB, we can perform DB transactions. Since
these DB transactions are dependent on the values and variables of the host
language. Depending on their values, query will be written and executed.
Similarly, results of DB query will be returned to the host language which will
be captured by the variables of host language. Hence we need to declare the
variables to pass the value to the query and get the values from query.
There are two types of variables used in the host language as follows
1. Host Variable
2. Indicator Variable
Host Variable
These are the variables of host language used to pass the value to the query as
well as to capture the values returned by the query. When host variables are
used in a SQL query, it should be preceded by colon – ‘:’ to indicate that it is a
host variable. Suppose we do not know what should be the datatype of host
variables or what is the datatype in oracle for few of the columns. In such case
we can allow the compiler to fetch the datatype of column and assign it to the
host variable. It is done using ‘BASED ON’ clause.

EXEC SQL BEGIN DECLARE SECTION;


BASED ON STUDENT.STD_ID sid;
BASED ON STUDENT.STD_NAME sname;
BASED ON STUDENT.ADDRESS saddress;
EXEC SQL END DECLARE SECTION;

Indicator Variable
These variables are also host variables but are of 2 byte short type always.
These variables are used to capture the NULL values that a query returns. If we
have to capture the NULL values for each host variable in the code, then we
have to declare indicator variables to each of the host variables. These indicator
variables are placed immediately after the host variable in a query or separated
by INDICATOR between host and indicator variable.
EXEC SQL BEGIN DECLARE SECTION;
short ind_sid; // indicator variable
EXEC SQL END DECLARE SECTION;

Let’s Take an Example:


Consider a simple Pro*C program to illustrate embedded SQL. This
program below accepts student name from the user and queries DB for
his student id.

#include <stdio.h>
#include <sqlca.h>

int main ( )
{
EXEC SQL INCLUDE SQLCA;

EXEC SQL BEGIN DECLARE SECTION;


BASED ON STUDENT.STD_ID SID; // host variable to store the value returned by query
char *STD_NAME; // host variable to pass the value to the query
short ind_sid; // indicator variable
EXEC SQL END DECLARE SECTION;

printf ("Enter the Student name:");


scanf("%s", STD_Name);

// Executes the query


EXEC SQL SELECT STD_ID INTO: SID INDICATOR ind_sid FROM STUDENT
WHERE STD_NAME =: STD_NAME;

// prints the result from DB


printf ("STUDENT ID: %d", SID);
exit (0);

}
In this embedded SQL, all the queries are dependent on the values of host
variable and queries are static. That means, in above example of SELECT
query, it always pulls student ID for the student name inserted. But suppose
user enters student ID instead of student name. Then these SQLs are not
flexible to modify the query to fetch details based on ID. Suppose query is based
on name and address of a student. Then code will not modify the query to fetch
details based on name and address of a student. That means queries are static
and it cannot be modified based on user input. Hence this kind of SQLs is
known as static SQLs.

Das könnte Ihnen auch gefallen