cobol embedded example

61
 A Guide to SQL, Fifth Edi tion 1 Chapter 8 Embedded SQL

Upload: rahul-jaiswal

Post on 04-Apr-2018

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 1/61

 A Guide to SQL, Fifth Edition

1

Chapter 8

Embedded SQL

Page 2: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 2/61

2

Objectives

Embed SQL commands in COBOLstatements

Retrieve single rows using embedded SQLUpdate a table using embedded INSERT,UPDATE, and DELETE commands

Use cursors to retrieve multiple rows in

embedded SQLUpdate a database using cursors

Learn how to handle errors in programscontaining embedded SQL commands

Page 3: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 3/61

3

Introduction

Use a procedural language to useembedded SQL commands to

accomplish tasks beyond thecapabilities of SQL

Use EXEC SQL and END-SQL in

COBOL programming to distinguishembedded SQL commands fromstandard COBOL statements

Page 4: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 4/61

4

Introduction

DATA DIVISION (WORKING-STORAGE SECTION) contains

statements declaring the tables that willbe used in processing the database

Communications area includes items

that allow SQL to communicate variousaspects of processing to the program

Page 5: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 5/61

5

Introduction

SQLCODE contains a code indicatingthe fate of the executed statement

Normal = zero

Not normal = value in SQLCODE thatindicates problem

PROCEDURE DIVISION contain newstatements that will be SQL statementswith slight variations

Page 6: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 6/61

6

DATA DIVISION

Tables to be processed are declared inWORKING-STORAGE

Use the DECLARE TABLE command

Page 7: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 7/61

7

Create SALES_REP Table

EXEC SQL

DECLARE SALES_REP TABLE

(SLSREP_NUMBER DECIMAL (2),

LAST CHAR (10),FIRST CHAR (8),

STREET CHAR (15),

CITY CHAR (15),

STATE CHAR (2),

ZIP_CODE CHAR (5),TOTAL_COMMISSION DECIMAL (7,2),

COMMISSION_RATE DECIMAL (3,2) )

END-EXEC.

Page 8: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 8/61

8

Code for Using Library

EXEC SQL

INCLUDE DECSALES_REP

END-EXEC.

Page 9: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 9/61

9

COBOL Variables

01 W-SALES-REP.

03 W-SLSREP_NUMBER PIC S9(2) COMP-3.

03 W-LAST PIC X(10).

03 W-FIRST PIC X(8).03 W-STREET PIC X(15).

03 W-CITY PIC X(15).

03 W-STATE PIC X(2).

03 W-ZIP-CODE PIC X(5).03 W-TOTAL-COMMISSION PIC S9(5)V9(2) COMP-3.

03 W-COMMISSION-RATE PIC S9V9(2) COMP-3.

Page 10: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 10/61

10

SQLCA

SQL communication area providesfeedback to the program

Example:

EXEC SQL

INCLUDE SQLCA

END-EXEC.

Page 11: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 11/61

11

PROCEDURE DIVISION

Use Host variables

Proceed the variable with a colon

Replace results of SQL queries in hostvariables in the INTO CLAUSESELECT LAST

INTO :W-LAST

FROM SALES_REPWHERE SLSREP_NUMBER = “03” 

Make provisions for exceptional conditions

Page 12: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 12/61

12

Example 1

Obtain the last name of sales repnumber 03 and place it in W-LAST

Page 13: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 13/61

13

Retrieve a Single Row and

ColumnIn SQL:SELECT LAST

FROM SALES_REP

WHERE SLSREP_NUMBER = “03”; 

In COBOL:EXEC SQL

SELECT LAST

INTO :W-LASTFROM SALES_REP

WHERE SLSREP_NUMBER = “03” 

END-EXEC.

Page 14: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 14/61

14

Example 2

Obtain all information about the salesrep whose number is stored in the host

variable W-SLSREP-NUMBER

Page 15: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 15/61

15

Retrieve a Single Row and All

ColumnsEXEC SQL

SELECT LAST, FIRST, STREET, CITY, STATE, ZIP_CODE,TOTAL_COMMISSION, COMMISSION RATE

INTO :W-LAST, :W-FIRST, :W-STREEET, :W-CITY,:W-STATE, :W-ZIP-CODE, :W-TOTAL-COMMISSION,

:W-COMMISSION-RATE

FROM SALES_REP

WHERE SLSREP_NUMBER = :W-SLSREP-NUMBER

END-EXEC.

Page 16: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 16/61

16

Program to Display Sales Rep

Information

Page 17: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 17/61

17

Program to Display Sales Rep

Information (version 2)

Page 18: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 18/61

18

Example 3

Obtain the last name, first name, andaddress of the customer whose

customer number is stored in the hostvariable, W-CUSTOMER-NUMBER, aswell as the number, last name, and first

name of the sales rep who representsthis customer 

Page 19: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 19/61

19

Retrieve a Single Row from a JoinEXEC SQL

SELECT CUSTOMER.LAST, CUSTOMER.FIRST,CUSTOMER.STREET, CUSTOMER.CITY,CUSTOMER.STATE, CUSTOMER.ZIP_CODE,CUSTOMER.SLSREP_NUMBER, SALES_REP.LAST,SALES_REP.FIRST

INTO :W-LAST OF :W-CUSTOMER,

:W-FIRST OF :W-CUSTOMER, :W-STREET OF:W-CUSTOMER, :W-CITY OF :W-CUSTOMER,

:W-STATE OF :W-CUSTMOER, :W-ZIP-CODE OF

:W-CUSTOMER, :W-SLSREP-NUMBER OF

:W-CUSTOMER, :W-LAST OF :W-SLAES-REP,

:W-FIRST OF :W-SALES-REP

FROM SALES-REP, CUSTOMER

WHERE SALES-REP.SLSREP_NUMBER =CUSTOMER.SLSREP_NUMBER

 AND CUSTOMER.CUSTOMER_NUMBER =

:W-CUSTOMER-NUMBER

END-EXEC.

Page 20: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 20/61

20

Example 4

 Add a row to the SALES_REP table

The sales rep number, last name, first name

address, total commission, and credit limitalready have been placed in the variables W-SLSREP-NUMBER, W-LAST, W-FIRST, W-STREET, W-CITY, W-STATE, W-ZIP-CODE,

W-TOTAL-COMMISION, and W-COMMISSION-RATE, respectively.

Page 21: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 21/61

21

Insert a Row Into a Table

EXEC SQL

INSERT

INTO SALES_REP

VALUES (:W-SLSREP-NUMBER, :W-LAST, :W-FIRST,:W-STREET, :W-CITY, :W-STATE, :W-ZIP-CODE,

:W-TOTAL-COMMISSION, :W-COMMISSION-RATE)

END-EXEC.

Page 22: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 22/61

22

Program to Add Sales Reps

Page 23: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 23/61

23

Example 5

Change the last name of the sales repwhose number currently is stored in W-

SLSREP-NUMBER to the valuecurrently stored in W-LAST

Page 24: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 24/61

24

Change A Single Row In A

TableEXEC SQL

UPDATE SALES_REP

SET LAST = :W-LAST

WHERE SLSREP_NUMBER = :WSLSREP-NUMBEREND-EXEC.

Page 25: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 25/61

25

Example 6

 Add the amount stored in the hostvariable INCREASE-IN-RATE to the

commission rate for all sales reps whocurrently represent any customer havinga credit limit of $1,000

Page 26: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 26/61

26

Change Multiple Rows in a

TableEXEC SQL

UPDATE SALES_REP

SET COMMISSION_RATE = COMMISSION_RATE +

:INCREASE-IN-RATEWHERE SLSREP_NUMBER IN

(SELECT SLSREP_NUMBER

FROM CUSTOMER

WHERE CREDIT_LIMIT = 1000)

END-EXEC.

Page 27: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 27/61

27

Example 7

Delete the sales rep whose number currently is stored in W-SLSREP-

NUMBER from the SALES_REP table

Page 28: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 28/61

28

Delete a Single Row From a

TableEXEC SQL

DELETE

FROM SALES_REP

WHERE SLSREP_NUMBER = :W-SLSREP-NUMBEREND-EXEC.

Page 29: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 29/61

29

Example 8

Delete every order line for the order whose order number currently is stored

in the host variable W-ORDER-NUMBER from the ORDER_LINE table

Page 30: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 30/61

30

DELETE Multiple Rows From

a TableEXEC SQL

DELETE

FROM ORDER_LINE

WHERE ORDER_NUMBER = :W-ORDER-NUMBEREND-EXEC.

Page 31: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 31/61

31

Multiple-Row SELECT

Example of SELECT statement producingmultiple rowsEXEC SQL

SELECT CUSTOMER_NUMBER, LAST, FIRSTINTO :W-CUSTOMER-NUMBER, :W-LAST, :W-FIRST

FROM CUSTOMER

WHERE SLSREP_NUMBER = :W-SLSREP-NUMBER

END-EXEC

Problem: COBOL can process only one record at a time,

where this SQL command produces multiple rows(records)

Page 32: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 32/61

32

Cursors

Cursor 

 A pointer to a row in the collection of rowsretrieved by a SQL statement

 Advances one row at time to provide sequential,record-at-a-time access to the retrieved rows soCOBOL can process the rows

Using a cursor, COBOL can process the set of retrieved rows as though they were records in asequential file.

Page 33: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 33/61

33

Example 9

Retrieve the number, last name, andfirst name of every customer 

represented by the sales rep whosenumber is stored in the host variable W-SLSREP-NUMBER

Page 34: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 34/61

34

Using a Cursor 

EXEC SQL

DECLARE CUSTGROUP CUROSR FOR

SELECT CUSTOMER_NUMER, LAST, FIRST

FROM CUSTOMERWHERE SLSREP_NUMBER = :W-SLSREP-NUMBER

END-EXEC.

Page 35: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 35/61

35

OPEN, FETCH,CLOSE

OPEN, FETCH, and CLOSE commandsare used in processing a cursor 

 Analogous to the OPEN, READ, andCLOSE commands used in processinga sequential file

Page 36: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 36/61

36

Opening a Cursor 

EXEC SQL

OPEN CUSTGROUP

END-EXEC.

Page 37: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 37/61

37

Before OPEN

Page 38: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 38/61

38

 After OPEN, But Before First

FETCH

Page 39: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 39/61

39

Fetching Rows from a Cursor 

EXEC SQL

FETCH CUSTGROUP

INTO :W-CUSTOMER-NUMBER, :W-LAST, :W-FIRST

END-EXEC.

Page 40: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 40/61

40

 After First FETCH

Page 41: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 41/61

41

 After Second FETCH

Page 42: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 42/61

42

 After Third FETCH

Page 43: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 43/61

43

 After Attempting a Fourth

FETCH

Page 44: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 44/61

44

Closing a Cursor 

EXEC SQL

CLOSE CUSTGROUP

END-EXEC.

Page 45: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 45/61

45

 After CLOSE

Page 46: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 46/61

46

Program to Display Customers of a

Given Sales Rep

Page 47: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 47/61

47

Example 10

For every order that contains an order line for the part whose part number is stored in W-PART-NUMBER, retrieve the order number,order date, last name, and first name of thecustomer who placed the order, and thenumber, last name, and first name of the

sales rep who represent the customer Sort the results by customer number 

Page 48: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 48/61

48

More Complex CursorsEXEC SQL

DECLARE ORDGROUP CURSOR FOR

SELECT ORDERS.ORDER_NUMBER,ORDERS.ORDER_DATE,CUSTOMER.CUSTOMER_NUMBER, CUSTOMER.LAST,FIRST, SALES_REP.SLSREP_NUMBER, SALES_REP.LAST,

FIRSTFROM ORDER_LINE, ORDERS, CUSTOMER, SALES_REP

WHERE ORDER_LINE.PART_NUMBER = :W-PART-NUMBER

 AND ORDER_LINE.ORDER_NUMBER =ORDERS.ORDER_NUMBER

 AND ORDERS.CUSTOMER_NUMBER =CUSTOMER.CUSTOMER_NUMBER

 AND CUSTOMER.SLSREP_NUMBER =SALES_REP.SLSREP_NUMBER

ORDER BY CUSTOMER.CUSTOMER_NUMBER

END-EXEC.

Page 49: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 49/61

49

 Advantages of Cursors

The coding the in the program is greatly simplified

 A special component of the database managementsystem called the optimizer determines the best way

to access the data

If the database structure changes in such a way thatthe necessary information is still obtainable using adifferent query, the only change required in the

program is the cursor definition in WORKING-STORAGE (PRODECURE DIVISION code is notaffected)

Page 50: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 50/61

50

Example 11

 Add $100 to the credit limit for every customer represented by the sales rep whose number currentlyis stored in the host variable W-SLSREP-NUMBER,

whose balance is not over the credit limit, and whosecredit limit is $500 or less

 Add $200 to the credit limit of every customer of thissales rep whose balance is not over the credit limitand whose credit limit is more than $500

Write the number, last name, and first name of everycustomer of this sales rep whose balance is greater than the credit limit

Page 51: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 51/61

51

Updating Cursors

EXEC SQL

DECLARE CREDGROUP CURSOR FOR

SELECT CUSTOMER_NUMBER, LAST, FIRST,

BALANCE, CREDIT_LIMITFROM CUSTOMER

WHERE SLSREP_NUMBER = :W-SLSREP-NUMBER

FOR UPDATE OF CREDIT-LIMIT

END-EXEC.

Page 52: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 52/61

52

Code to FETCH a RowEXEC SQL

FETCH CREDGROUP

INTO :W-CUSTOMER-NUMBER, :W-LAST, :W-FIRST, :W-BALANCE, :W-CREDIT-LIMIT

END-EXEC

IF SQLCODE = 100

MOVE “NO” TO ARE-THERE-MORE-CUSTOMERS

ELSE

PERFORM CUSTOMER-UPDATE.

CUSTOMER-UPDATE.

IF W-CREDIT-LIMIT > W-BLANCE

DISPLAY W-FIRST, W-LAST

ELSE IF W-CREDIT-LIMIT > 500

EXEC SQL

UPDATE CUSTOMER

SET CREDIT-LIMIT = CREDIT_LIMIT + 200

WHERE CURRENT OF CREDGROUP

END-EXEC

ELSE

EXEC SQL

UPDATE CUSTOMER

SET CREDIT_LIMIT = CREDIT_LIMIT + 100

WHERE CURRENT OF CREDGROUP

END-EXEC.

Page 53: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 53/61

53

Error Handling

Two types of error conditions

Unusual but normal conditions

 Abnormal and unexpected conditions or fatal errors.

Page 54: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 54/61

54

Unusual Conditions Errors

The value in SQLCODE will be a positivenumber 

 Appropriate action is to print an error message and continue processing

 Appropriate action for END OF DATA(SQLCODE-100) is termination of some loop

and continuation with the rest of the programNo error message is required

Page 55: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 55/61

55

 Abnormal and Unexpected

ErrorsValue in SQLCODE is a negativenumber 

 Appropriate action is to print a finalmessage that indicates the problem andterminate the program

Use the WHENEVER statement tohandle these errors in a global way

Page 56: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 56/61

56

WHENEVER Statement

EXEC SQL

WHENEVER SQLERROR GOTO ERROR-PROCESSING-ROUTINE

END-EXEC.

EXEC SQL

WHENEVER SQLWARNING CONTINUE

END-EXEC.

EXEC SQL

WHENEVER NOT FOUND CONTINUE

END-EXEC.

Page 57: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 57/61

57

Error Codes

SQLERROR = abnormal or fatalcondition (SQLCODE < 0)

SQLWARNING = unusual but normalconditions (SQLCODE > 0)

NOT FOUND = special warning END

OF DATA (SQLCODE =100)

Page 58: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 58/61

58

Summary

To embed SQL commands in a COBOL program,precede the SQL command with EXEC SQL, andfollow the command with END-EXEC

Statements to define the tables to be accessed mustappear in the DATA DIVISION

The DATA DIVISION must contain the INCLUDESQLCA statement, which allows access to the SQLcommunication area

You can use host language variables (variables thatare not columns within a table) in embedded SQLcommands by preceding the variable name with acolon

Page 59: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 59/61

59

Summary

You can use SELECT statements as embedded SQLcommands in COBOL programs only when a singlerow is retrieved

To place the results of a SELECT statement into hostlanguage variables, use the INTO clause in theSELECT command

You can use INSERT, UPDATE, and DELETEstatements in COBOL programs, even when they

affect more than one rowIf a SELECT statement is to retrieve more than onerow, it must be used to define a cursor that will beused to select one row at a time

Page 60: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 60/61

60

Summary

To activate a cursor, use the OPEN command toexecute the query in the cursor definition

To select the next row in COBOL, use the FETCH

commandTo deactivate a cursor, use the CLOSE command.The rows initially retrieved will not longer be availableto COBOL

DATA in the tables on which a cursor is based can beupdated by including the WHERE CURRENT OFcursor name clause in the update statement This clause updates only the current (most recently fetched)

row

Page 61: Cobol Embedded Example

7/30/2019 Cobol Embedded Example

http://slidepdf.com/reader/full/cobol-embedded-example 61/61

Summary

To see if an error has occurred, examine the value inSQLCODE

Rather than checking SQLCODE in every place in

the program where errors could occur, use theWHENEVER clause