EMBEDDED SQL

If you have any queries please leave a message here
Your Message
×


The structured query language provides us 2 features :

Actually all the queries can’t be expressed in SQL alone. There are many queries that are expressed in programming languages like C, C++, Java but can’t be expressed in SQL. For writing such queries we need to embed the SQL in general purpose programming languages. The mixture of SQL and general purpose programming languages is called embedded SQL.


CONNECTION ESTABLISHMENT

Embedded SQL provides multiple ways in which to include code for establishing database connections. Depending on the embedded SQL host programming language there might be one or more way of doing this.

Connecting to DB2 databases in C and C++ Embedded SQL applications

When working with C and C++ applications, a database connection can be established by executing the following statement.

EXEC SQL CONNECT TO sample;

If you want to use a specific user id (test) and password (mypassword), use the following statement:

EXEC SQL CONNECT TO sample USER test USING mypassword;


HOST VARIABLES

Host variables are variables referenced by embedded SQL statements. They are used to exchange data values between the database server and the embedded SQL application. Embedded SQL applications can also include host variable declarations for relational SQL queries.

Host variables are declared using the host language specific variable declaration syntax in a declaration section.

A declaration section is the portion of an embedded SQL application found near the top of an embedded SQL source code file, and is bounded by two non-executable SQL statements :

These statements enable the precompiler to find the variable declarations. Each host variable declaration must appear in between these two statements, otherwise the variables are considered to be only regular variables.

EXAMPLE

EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[50];
double salary;
EXEC SQL END DECLARE SECTION;


EMBEDDED SQL EXAMPLE

CREATE Operation

EXEC SQL CREATE TABLE employee(emp_id number(4) PRIMARY KEY, emp_name varchar(50), salary number(8,2));

INSERT Operation

EXEC SQL INSERT INTO employee VALUES(1024,'Debabrata',15000);

SELECT Operation

SELECT emp_name,emp_salary INTO :name, :salary FROM employee WHERE emp_id = :id;

EXAMPLE 1

//SQL Variables
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[50];
double salary;
EXEC SQL END DECLARE SECTION;

//Input employee id from user
printf (“\nEnter the employee id . . .”);
scanf (“%d”, &id);

//Execute the SQL query
EXEC SQL SELECT emp_name,emp_salary INTO :name, :salary FROM employee WHERE emp_id = :id;

//Display the results
printf("%d \t %s \t %lf", id, name, salary);
exit(0);


EXAMPLE 2

//SQL Variables
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[50];
double salary;
EXEC SQL END DECLARE SECTION;

//Cursor declaration
EXEC SQL DECLARE record CURSOR FOR SELECT emp_id, emp_name, emp_salary FROM employee;

//Fatch data from the cursor
EXEC SQL OPEN record;
do
{
EXEC SQL FETCH record INTO :id, :name, :salary;
if ( SQLCODE != 0 )
break;
printf("%d \t %s \t %lf", id, name, salary);
}
while(1);
EXEC SQL CLOSE record;






DYNAMIC SQL

Dynamic SQL is a programming methodology for generating and running SQL statements at run time.

EXAMPLE :

Considered the table :
employee (emp_id, emp_name, emp_salary)

//SQL Variables
EXEC SQL BEGIN DECLARE SECTION;
char condition[50];
char query[200];
EXEC SQL END DECLARE SECTION;

//Input where conditions
printf (“\nEnter the where condition . . .”);
scanf (“%s”, &condition);

//Plus is used as a symbol for concatenation operator
query="SELECT * FROM employee WHERE "+condition;

EXEC SQL PREPARE sqlcommand FROM :query;
EXEC SQL EXECUTE sqlcommand;



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us