Day22embedded SQL New

38
EMBEDDED SQL " An SQL Application program is written in a " HOST LANGUAGE with embedded SQL " statements. " DB2 supports the following host languages. " COBOL " PL/1 " FORTRAN " ASSEMBLY LANGUAGE

Transcript of Day22embedded SQL New

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 1/38

EMBEDDED SQL

" An SQL Application program is written in a" HOST LANGUAGE with embedded SQL" statements.

" DB2 supports the following host languages." COBOL" PL/1" FORTRAN" ASSEMBLY LANGUAGE

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 2/38

EMBEDDED SQL" DELIMITING SQL STATEMENTS"

" Coding SQL statements in a COBOL application" requires beginning and ending delimeters.

" The delimeters are EXEC SQL & END-EXEC." FOR MAT"

EXEC SQL" any SQL statement" END-EXEC.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 3/38

EMBEDDED SQL

" Steps to be followed:"

"

1. Declaring SQL Communication Area(SQLCA)" 2. Declaration of the table to used with in" application program." 3. Host Variable declaration

4. Coding SQL statements to manipulate DB2 data

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 4/38

EMBEDDED SQL

" SQL C O MMUNICATI O N A R EA (SQLCA)"

" DB2 program communication is accomplished"

through the SQL communication Area." SQLCA provides the fields set by DB2 after the" SQL statement execution.

" The return code placed in the field SQLCODE" It indicates the success,failure or exception" encountered.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 5/38

EMBEDDED SQL

" DECLA R ING SQLCA

" The C O BO L description of SQLCA:

" 0 1 SQLCA05 SQLCAID PIC X(8) VALUE 'SQLCA'.05 SQLCABC PIC S9(9) COMP.05 SQLCODE PIC S9(9) COMP.05 SQLERRM.

" 49 SQLERRML PIC S9(4) COMP." 49 SQLERRMC PIC X(7 0 ).

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 6/38

EMBEDDED SQL

" 05 SQLERRP PIC X(8)." 05 SQLERRD PIC S9(9) COMP OCCURS 6" TIMES ." 05 SQLWARN.

10

SQLWARN0

PIC X(1).10 SQLWARN1 PIC X(1).10 SQLWARN2 PIC X(1).10 SQLWARN3 PIC X(1).10 SQLWARN4 PIC X(1).

10 SQLWARN 5 PIC X(1).10 SQLWARN6 PIC X(1).10 SQLWARN7 PIC X(1).

05 SQLEXT PIC X(8).

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 7/38

EMBEDDED SQL" DECLA R ING SQLCA

" An SQLCA must be declared in the" WORKING STORAGE SECTION of every" DB2/SQL COBOL program.

" Declaration of SQLCA can be done in two ways:

" 1.Coding the COBOL description of SQLCA directly.

" 2.Coding the SQL statement." EXEC SQL" INCLUDE SQLCA" END -EXEC.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 8/38

EMBEDDED SQL" DECLA R ING DB2 TABLES

" DB2 tables to be used in a COBOL program" must be declared in the WORKING-STORAGE" SECTION. Two ways of declaring tables" 1. Coding the table declaration directly,using the" DECLARE TABLE statement." EXEC SQL" DECLARE EMPTAB TABLE" (EMPID INT NOTNULL," EMPNAME CHAR(1 0 )," EMPADD VARCHAR(2 0 )," EMPSAL DECEMIAL(8,2)) END-EXEC.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 9/38

EMBEDDED SQL

" 2. Using DCLGEN (Declare Generation) to generate" the table declaration and then copying the same" into the program in the WORKING-STORAGE" SECTION using the INCLUDE statement.

EXEC SQLINCLUDE EMPTAB1

END-EXEC.

" EMPTAB1 is the member of a partitioned data set" generated by DCLGEN and contains the" declaration of table EMPTAB.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 10/38

EMBEDDED SQL" H O ST VA R IABLES" Are defined in the WORKING-STORAGE" SECTION of the" DATA DIVISION just like any other data items" 0

1 HV-EMP-REC .0 2 HV-EMPID PIC S9(9) comp.0 2 HV-EMPNAME PIC X(1 0 )0 2 HV-ADD.

49 HV-ADD-LEN PIC S9(4) comp.49 HV-ADD-VAL PIC X(2 0 ).

0 2 HV-SAL PIC S9(6)V9(2) COMP-3.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 11/38

EMBEDDED SQL" H O ST VA R IABLES

" 1 Are preceded by a COLON when used in SQL" statements to signify that they are host variables"

and not SQL variables."

" EXEC SQL" SELECT EMPID, EMPNAME" INTO : HV-EMPID, :HV-EMPNAME" FROM EMPTAB" WHERE EMPID = : HV-EMPID" END-EXEC.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 12/38

EMBEDDED SQL

"

HO

ST VAR

IABLES" 2 Are just regular COBOL data items when

used in a non-SQL

" COBOL statement.

" MOVE INPUT-EMPID TO HV-EMPID."

DISPLAY HV-EMPNAME.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 13/38

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 14/38

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 15/38

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 16/38

EMBEDDED SQL

" C O DING SQL T O MANIPULATE DB2DATA"

Considering the following SELECT statement:"

EXEC SQLSELECT EMPID,EMPNAMEINT O :HV-EMPID,:HV-EMPNAMEFRO M EMPTABWHE R E EMPID =:HV-EMPID

END-EXEC.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 17/38

EMBEDDED SQL

" Each evaluation of the Select statement must returns onlyONE row from the table EMPTAB." DB2 places the retrieved data directly into the host

variables.

" If it is return more then one row then, the Select statementreturn Error message. The Error code is

" -811 (SQLCODE)" Note : The row should not have any NULL data

" If it is return any column has NULL data then, the Selectstatement return Error message. The Error code is

" -305 (SQLCODE)

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 18/38

EMBEDDED SQL" A SIMPLE DB2/SQL C O BO L P RO G R AM" IDENTIFICATION DIVISION." PROGRAM-ID. SAMPLE1." ENVIRONMENT DIVISION." DATA DIVISION." WORKING-STORAGE SECTION." EXEC SQL" INCLUDE SQLCA" END-EXEC.

"

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 19/38

EMBEDDED SQL"

" EXEC SQLDECLARE EMPTAB TABLE

(ECODE CHAR(4),ENAME CHAR(10

),EADD CHAR (2 0 ))

" END-EXEC

" 0 1 HV-EMP-REC." 05 HV-ECODE PIC X(4)." 05 HV-ENAME PIC X(1 0 )." 05 HV-EADD PIC X(2 0 ).

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 20/38

EMBEDDED SQL

" * " PROCEDURE DIVISION." 000 1-MAIN-PARA.

DISPLAY "Enter the Ecode".

ACCEPT HV-ECODE.

DISPLAY "Enter the Ename".

ACCEPT HV-ENAME.

DISPLAY "Enter the Eadd ".ACCEPT HV-EADD.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 21/38

" EXEC SQLINSERT INTO EMPTAB

(ECODE,ENAME,EADD)

VALUES(:HV-ECODE,:HV-ENAME,

:HV-EADD)

" END-EXEC." IF SQLCODE= 0 "

" DISPLAY "RECORD ADDED"" ELSE" DISPLAY "ERROR OCCURED""

STOP RUN

EMBEDDED SQL

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 22/38

EMBEDDED SQL

" DB2 Application Program Preparation/Execution

Precompiler

Bind

DBRM

Sourcemodule

ModifiedSourceModule

Compiler

(One per sourcemodule)

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 23/38

EMBEDDED SQL - 47" DB2 Application Program Preparation/Execution

Object

Module Listof

packages

PackageCollection

Of Packages Bind

Objectmodule

Other

Object

ModulesLinkage

Editor

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 24/38

EMBEDDED SQL - 47

" DB2 Application Program Preparation/Execution

Application

Plan

Load

Module(Load Module)

(Application Plan/Packages)

RuntimeSuperviser DataManager Buffer Manager (Other)

DB

MainMemory

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 25/38

The µPRECOMPILE¶ Process

" The PRECOMPILE process involves taking the" COBOL Source code with the SQL statements" embedded in it and splitting it into:"

1. A modified source code with the SQL" commands Being replaced by equivalent Host" Language µCALL¶ statements." 2. A DBRM (DataBase Request Module) which" Consists of the SQL statements that are removed" from the Original Source code." 3.It also produces a Listing for the" precompilation process.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 26/38

The µPRECOMPILE¶ Process

" The precompiler validates the DBRM and" performs syntax checking on all the SQl" statements used in the Application."

The µModified source code¶ obtained from the" Precompiler is compiled, link edited form which" a LOAD module is obtained." This is Load module is executed at run time.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 27/38

The µBIND¶ Process" The BIND is actually an µ optimizing compiler .¶

" It converts high level Database requests into

" optimized internal form.

" It compiles the SQL statements into µcode¶ even" though that code is actually not truly machine" code.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 28/38

The µBIND¶ Process" There are two major function of the BIND" process." 1. Compile and BIND the given DBRM¶s to" form a PACKAGE.

" 2. BIND a PACKAGE or a Collection of such" Packages to form a APPLICATION PLAN.

"

The listing for binding the DBRM¶s into a" package and binding the Packages to form a Plan" are produced separately and transferred to the" spool.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 29/38

The µBIND¶ Process

" There are two major function of the BIND" process." SYNTAX CHECKING" Bind examines the SQL statements in the input" DBRM, parses them and reports on any syntax" errors it finds." Even though the precompiler performs the same" functions, as it is decoupled from the rest of the" DB2 system, the BIND function cannot assume" that the DBRM is valid.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 30/38

The µBIND¶ Process

" DATABASE REQUEST VALIDATION" Bind validates all the tables, views, and columns" used in the program." If any of the above objects is not present in the" DB2 system, the BIND process will" terminate abnormally." If any index is used for retrieval of data, and the" index has been dropped, BIND achieves some" other path for retrieving the same data.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 31/38

EMBEDDED SQL" P R EPA R ING A DB2/SQL C O BO L" P RO G R AM FOR EXECUTI O N" PACKAGE" Produced by binding DBRMs." Consists of a set of internal structures," representing the compiled form of the original" SQL statements in the corresponding DBRMs" Physically stored in the DB2 directory.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 32/38

EMBEDDED SQL

" C O LLECTI O N" A name given to logically-related set of " packages." Does not have any physical existence of its own" Typically (but not necessarily) all of the" packages used in a given application would be" assigned to the same collection.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 33/38

EMBEDDED SQL" Plan" It is a DB2 object produced b during the BIND" Processes that combines one or more DBRM¶s." Essentially, a plan contains, a list of the names of " packages needed to execute the application." Packages and collections" Each package is assigned to exactly one" collection when it is created." Collections provide a useful "level of " indirection" between packages and plans.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 34/38

EMBEDDED SQL" F

inal Execution" Since the original program has been broken ito" Two, It must now somehow be brought back into" One piece at execution time." First the Cobol load module starts to execute the" usual way. When the first call to the DB2" language interface is reached, the control is" passed to the RUNTIME SUPERVISOR.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 35/38

EMBEDDED SQL" F

inal Execution" The Runtime supervisor then retrieves the" application plan from the DB2 directory, loads" them into memory, and uses the information" they contain to request the data from the DATA" MANAGER." The DATA MANAGER passes the" corresponding data to the host program using the" buffer manager.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 36/38

EMBEDDED SQL" R

UNTIME SUPEVISOR

" The Runtime supervisor is resident in main" memory when the Application program is" Executing, to oversee that execution." Is the first to get control, when" Application program requests database operation" to be performed." Uses control information to request data on the" part of the DATA MANAGER.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 37/38

EMBEDDED SQL"

DATA MANAGER

" Is the component that manages the physical" Databases by performing all of the normal access" method functions, like search, retrieval etc." Invites other system components to perform" operations such as locking logging I/O" operations etc during its task.

8/8/2019 Day22embedded SQL New

http://slidepdf.com/reader/full/day22embedded-sql-new 38/38

EMBEDDED SQL"

BUFF

ER

MANAGER

" Is the component responsible for physically" transferring data between the external storage" (i.e) the database and the Main memory (i.e)" the running application.