Академический Документы
Профессиональный Документы
Культура Документы
This gap between application programs and SQL is bridged by the use of
embedded SQL and dynamic SQLs. These SQLs provide the utility to use SQLs
inside the application 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.
Embedded SQL
Like we said above, 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. There are
different host languages which support embedding SQL within it like C, C++,
ADA, Pascal, FORTRAN, Java etc. 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. Let us discuss below embedded SQL with
respect to C 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.
Declaration Section
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. Since SQL is dependent on host language we have to use
variables of host language and such variables are known as host
variable. But these host variables should be declared within the SQL
area or within SQL code. That means compiler should be able to
differentiate it from normal C variables. Hence we have to declare host
variables within BEGIN DECLARE and END DECLARE section. Again,
these declare block should be enclosed within EXEC SQL and ‘;’.
We can note here that variables are written inside begin and end block of the
SQL, but they are declared using C code. It does not use SQL code to declare
the variables. Why? This is because they are host variables – variables of C
language. Hence we cannot use SQL syntax to declare them. Host language
supports almost all the datatypes from int, char, long, float, double, pointer,
array, string, structures etc.
When host variables are used in a SQL query, it should be preceded by colon
– ‘:’ to indicate that it is a host variable. Hence when pre-compiler compiles
SQL code, it substitutes the value of host variable and compiles.
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. But format of declaration will be in host
language.
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 or to INSERT/ UPDATE any NULL
values to the tables. When it is used in a SELECT query, it captures
any NULL value returned for any column. When used along with
INSERT or UPDATE, it sets the column value as NULL, even though
the host variable has value. 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.
Or
UPDATE STUDENT
SET ADDRESS = :STD_ADDR :IND_SADDR; --Sets NULL to ADDRESS
Execution Section
This is the execution section, and it contains all the SQL queries and
statements prefixed by ‘EXEC SQL’.
UPDATE STUDENT
SET ADDRESS = :STD_ADDR
WHERE STD_ID = :SID;
Error Handling
struct sqlca {
/* ub1 */ char sqlcaid [8];
/* b4 */ long sqlabc;
/* b4 */ long sqlcode;
struct {
/* ub2 */ unsigned short sqlerrml;
/* ub1 */ char sqlerrmc[70];
} sqlerrm;
….
long sqlcode; //returns the error code
…
char sqlstate [6]; //returns predefined error statements
….
}
If we have to use this error handling structure, then we have to include sqlca.h
header file in the program, using #include directives. In this structure mainly
SQLCODE and SQLSTATE are used to see the type of error. SQLCODE
returns different values for different types of errors.
Whenever error occurs in the code, then we have to redirect the execution of
code to handle the error rather than executing further. This is done using
WHENEVER statement.
#include <stdio.h>
#include <sqlca.h>
int main(){
EXEC SQL INCLUDE SQLCA;
//Error handling
EXEC WHENEVER NOT FOUND GOTO error_msg1;
EXEC WHENEVER SQLERROR GOTO error_msg2;
error_msg2:
printf("Error has occurred!");
printf("ERROR:%ld", sqlca->sqlcode);
printf("ERROR State:%s", sqlca->sqlstate);
exit(0);
}