UNIT II
EMBEDDED SQL – STATIC & DYNAMIC SQL
Embedded SQL
 Most SQL statements can be embedded in a general-
purpose host programming language such as COBOL, C,
Java
 An embedded SQL statement is distinguished from the
host language statements by enclosing it between EXEC
SQL or EXEC SQL BEGIN and a matching END-EXEC or
EXEC SQL END (or semicolon)
Syntax may vary with language
Shared variables (used in both languages) usually
prefixed with a colon (:) in SQL
COMMUNICATION BETWEEN THE PROGRAM AND
THE DBMS USING SQL CODE
The two communication variables that are used to communicate error
condition to the program are SQLCODE and SQLSTATE.
After each database command is executed, the DBMS returns a value in
SQLCODE, such that
SQLCODE=0 statement executed successfully
SQLCODE>0 no more data available
SQLCODE<0 some error has occured
Example: Variable Declaration
in Language C
 Variables inside DECLARE are shared and can appear (while
prefixed by a colon) in SQL statements
 SQLCODE is used to communicate errors/exceptions between the
database and the program
int loop;
EXEC SQL BEGIN DECLARE SECTION;
varchar dname[16], fname[16], …;
char ssn[10], bdate[11], …;
int dno, dnumber, SQLCODE, …;
EXEC SQL END DECLARE SECTION;
RETRIVING MULTIPLE TUPELS
 The query result table may contain more than one row. Consequently,
we must treat this as a multi-row query and use a cursor to retrieve the
data.
For that we have to set up a cursor to select the corresponding rows
from the table. After opening the cursor, we loop over each row of the
result table and print out the corresponding columns.
 When there are no more rows to be processed, we close the cursor and
terminate.
 If an error occurs at any point, we generate a suitable error message and
stop.
SQL Commands for
Connecting to a Database
 Connection (multiple connections are possible but only one is active)
CONNECT TO server-name AS connection-name
AUTHORIZATION user-account-info;
 Change from an active connection to another one
SET CONNECTION connection-name;
 Disconnection
DISCONNECT connection-name;
Embedded SQL in C
Programming Examples
loop = 1;
while (loop) {
prompt (“Enter SSN: “, ssn);
EXEC SQL
select FNAME, LNAME, ADDRESS, SALARY
into :fname, :lname, :address, :salary
from EMPLOYEE where SSN == :ssn;
if (SQLCODE == 0) printf(fname, …);
else printf(“SSN does not exist: “, ssn);
prompt(“More SSN? (1=yes, 0=no): “, loop);
END-EXEC
}
Embedded SQL in C
Programming Examples
 A cursor (iterator) is needed to process multiple tuples
 FETCH commands move the cursor to the next tuple
 CLOSE CURSOR indicates that the processing of query
results has been completed
prompt("Enter the Department Name: ", dname) ;
EXEC SQL
select Dnumber into :dnumber from DEPARTMENT where Dname = :dname ;
EXEC SQL DECLARE EMP CURSOR FOR
select Ssn, Fname, Minit, Lname, Salary from EMPLOYEE where Dno = :dnumber
FOR UPDATE OF Salary ;
EXEC SQL OPEN EMP ;
EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ;
while (SQLCODE == 0) {
printf("Employee name is:", Fname, Minit, Lname) ;
prompt("Enter the raise amount: ", raise) ;
EXEC SQL
update EMPLOYEE
set Salary = Salary + :raise
where CURRENT OF EMP ;
EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ;
}
EXEC SQL CLOSE EMP ;
STATIC SQL VS DYNAMIC SQL
 Static SQL is SQL statements in an application that do not change at runtime
and, therefore, can be hard-coded into the application.
 Static SQL provides performance advantages over dynamic SQL because static
SQL is pre-processed, which means the statements are parsed, validated, and
optimized only once.
 The full text of static SQL statements are known at compilation, which provides
the following benefits:
 Successful compilation verifies that the SQL statements reference valid
database objects.
 Successful compilation verifies that the necessary privileges are in place to
access the database objects.
 Performance of static SQL is generally better than dynamic SQL.
STATIC SQL VS DYNAMIC SQL
 Static SQL has limitations that can be overcome with dynamic SQL. You may not
always know the full text of the SQL statements that must be executed in a PL/SQL
procedure. Your program may accept user input that defines the SQL statements to
execute, or your program may need to complete some processing work to determine
the correct course of action. In such cases, you should use dynamic SQL.
 For example, consider a reporting application that performs standard queries on
tables in a data warehouse environment where the exact table name is unknown
until runtime.
 To accommodate the large amount of data in the data warehouse efficiently, you
create a new table every quarter to store the invoice information for the quarter.
These tables all have exactly the same definition and are named according to the
starting month and year of the quarter, for
example INV_01_1997, INV_04_1997, INV_07_1997, INV_10_1997, INV_01_1998, etc.
In such a case, you can use dynamic SQL in your reporting application to specify
the table name at runtime.
Dynamic SQL
 Dynamic SQL is SQL statements that are constructed at runtime; for
example, the application may allow users to enter their own queries.
Thus, the SQL statements cannot be hard-coded into the application.
 Objective:
 Composing and executing new (not previously compiled) SQL statements
at run-time
 a program accepts SQL statements from the keyboard at run-time
 a point-and-click operation translates to certain SQL query
 Dynamic update is relatively simple; dynamic query can be complex
 because the type and number of retrieved attributes are unknown at
compile time
DIFFERENCE BETWEEN STATIC AND
DYNAMIC SQL
 Static (embedded) SQL
 In static SQL how database
will be accessed is
predetermined in the
embedded SQL statement.
 It is more swift and efficient.
 Dynamic (interactive) SQL
 In dynamic SQL, how
database will be accessed is
determined at run time.
 It is less swift and efficient.
 Static (embedded) SQL
 SQL statements are compiled at
compile time.
 Parsing, validation, optimization,
and generation of application plan
are done at compile time.
 It is generally used for situations
where data is distributed uniformly.
 Execution IMMEDIATE,EXECUTE
and PREPARE statements are not
used.
 Dynamic (interactive) SQL
 SQL statements are compiled
at run time.
 Parsing, validation,
optimization, and generation
of application plan are done
at run time.
 It is generally used for
situations where data is
distributed non-uniformly.
 EXECUTE IMMEDIATE,
EXECUTE and PREPARE
statements are used.

embedded-static-&dynamic

  • 1.
    UNIT II EMBEDDED SQL– STATIC & DYNAMIC SQL
  • 2.
    Embedded SQL  MostSQL statements can be embedded in a general- purpose host programming language such as COBOL, C, Java  An embedded SQL statement is distinguished from the host language statements by enclosing it between EXEC SQL or EXEC SQL BEGIN and a matching END-EXEC or EXEC SQL END (or semicolon) Syntax may vary with language Shared variables (used in both languages) usually prefixed with a colon (:) in SQL
  • 3.
    COMMUNICATION BETWEEN THEPROGRAM AND THE DBMS USING SQL CODE The two communication variables that are used to communicate error condition to the program are SQLCODE and SQLSTATE. After each database command is executed, the DBMS returns a value in SQLCODE, such that SQLCODE=0 statement executed successfully SQLCODE>0 no more data available SQLCODE<0 some error has occured
  • 4.
    Example: Variable Declaration inLanguage C  Variables inside DECLARE are shared and can appear (while prefixed by a colon) in SQL statements  SQLCODE is used to communicate errors/exceptions between the database and the program int loop; EXEC SQL BEGIN DECLARE SECTION; varchar dname[16], fname[16], …; char ssn[10], bdate[11], …; int dno, dnumber, SQLCODE, …; EXEC SQL END DECLARE SECTION;
  • 5.
    RETRIVING MULTIPLE TUPELS The query result table may contain more than one row. Consequently, we must treat this as a multi-row query and use a cursor to retrieve the data. For that we have to set up a cursor to select the corresponding rows from the table. After opening the cursor, we loop over each row of the result table and print out the corresponding columns.  When there are no more rows to be processed, we close the cursor and terminate.  If an error occurs at any point, we generate a suitable error message and stop.
  • 6.
    SQL Commands for Connectingto a Database  Connection (multiple connections are possible but only one is active) CONNECT TO server-name AS connection-name AUTHORIZATION user-account-info;  Change from an active connection to another one SET CONNECTION connection-name;  Disconnection DISCONNECT connection-name;
  • 7.
    Embedded SQL inC Programming Examples loop = 1; while (loop) { prompt (“Enter SSN: “, ssn); EXEC SQL select FNAME, LNAME, ADDRESS, SALARY into :fname, :lname, :address, :salary from EMPLOYEE where SSN == :ssn; if (SQLCODE == 0) printf(fname, …); else printf(“SSN does not exist: “, ssn); prompt(“More SSN? (1=yes, 0=no): “, loop); END-EXEC }
  • 8.
    Embedded SQL inC Programming Examples  A cursor (iterator) is needed to process multiple tuples  FETCH commands move the cursor to the next tuple  CLOSE CURSOR indicates that the processing of query results has been completed
  • 9.
    prompt("Enter the DepartmentName: ", dname) ; EXEC SQL select Dnumber into :dnumber from DEPARTMENT where Dname = :dname ; EXEC SQL DECLARE EMP CURSOR FOR select Ssn, Fname, Minit, Lname, Salary from EMPLOYEE where Dno = :dnumber FOR UPDATE OF Salary ; EXEC SQL OPEN EMP ; EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ; while (SQLCODE == 0) { printf("Employee name is:", Fname, Minit, Lname) ; prompt("Enter the raise amount: ", raise) ; EXEC SQL update EMPLOYEE set Salary = Salary + :raise where CURRENT OF EMP ; EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary ; } EXEC SQL CLOSE EMP ;
  • 10.
    STATIC SQL VSDYNAMIC SQL  Static SQL is SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application.  Static SQL provides performance advantages over dynamic SQL because static SQL is pre-processed, which means the statements are parsed, validated, and optimized only once.  The full text of static SQL statements are known at compilation, which provides the following benefits:  Successful compilation verifies that the SQL statements reference valid database objects.  Successful compilation verifies that the necessary privileges are in place to access the database objects.  Performance of static SQL is generally better than dynamic SQL.
  • 11.
    STATIC SQL VSDYNAMIC SQL  Static SQL has limitations that can be overcome with dynamic SQL. You may not always know the full text of the SQL statements that must be executed in a PL/SQL procedure. Your program may accept user input that defines the SQL statements to execute, or your program may need to complete some processing work to determine the correct course of action. In such cases, you should use dynamic SQL.  For example, consider a reporting application that performs standard queries on tables in a data warehouse environment where the exact table name is unknown until runtime.  To accommodate the large amount of data in the data warehouse efficiently, you create a new table every quarter to store the invoice information for the quarter. These tables all have exactly the same definition and are named according to the starting month and year of the quarter, for example INV_01_1997, INV_04_1997, INV_07_1997, INV_10_1997, INV_01_1998, etc. In such a case, you can use dynamic SQL in your reporting application to specify the table name at runtime.
  • 12.
    Dynamic SQL  DynamicSQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries. Thus, the SQL statements cannot be hard-coded into the application.  Objective:  Composing and executing new (not previously compiled) SQL statements at run-time  a program accepts SQL statements from the keyboard at run-time  a point-and-click operation translates to certain SQL query  Dynamic update is relatively simple; dynamic query can be complex  because the type and number of retrieved attributes are unknown at compile time
  • 13.
    DIFFERENCE BETWEEN STATICAND DYNAMIC SQL  Static (embedded) SQL  In static SQL how database will be accessed is predetermined in the embedded SQL statement.  It is more swift and efficient.  Dynamic (interactive) SQL  In dynamic SQL, how database will be accessed is determined at run time.  It is less swift and efficient.
  • 14.
     Static (embedded)SQL  SQL statements are compiled at compile time.  Parsing, validation, optimization, and generation of application plan are done at compile time.  It is generally used for situations where data is distributed uniformly.  Execution IMMEDIATE,EXECUTE and PREPARE statements are not used.  Dynamic (interactive) SQL  SQL statements are compiled at run time.  Parsing, validation, optimization, and generation of application plan are done at run time.  It is generally used for situations where data is distributed non-uniformly.  EXECUTE IMMEDIATE, EXECUTE and PREPARE statements are used.