Top Banner

of 9

What Are DB2 Storage Areas

May 29, 2018

Download

Documents

sujijaya
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/9/2019 What Are DB2 Storage Areas

    1/9

    What are DB2 Storage Areas?

    When you write COBOL Programs, that read data records from Files, anresults to a File, you must declare Input and Output Storage Areas fthe COBOL Program.

    COBOL has a strict rule first Declare, then use. Look at the pictuFirstly whenever, you access data from Files in COBOL, you need

    http://lh6.ggpht.com/_sQvdFWqMlMg/TDkl2Lt0mnI/AAAAAAAAC7M/HOtgH3-iReA/s1600-h/Image20416.png
  • 8/9/2019 What Are DB2 Storage Areas

    2/9

    todeclare(announce) the file-names in COBOL. Moreover, you also declStorage Areas for sending data to and receiving data from files. TheArea is the place in the COBOL Program, where the data arrives and ga File, out here for the INPUT-FILE it is EMPLOYEE-INPUT-RECORD. WheINPUT-FILE, the input record QUASAR 1000 20-10-06 is read from the f

    received and stored in EMPLOYEE-INPUT-RECORD Storage Area. SimilarlyStorage Area is the place in the COBOL Program, from where the data output to the File, here for the OUTPUT-FILE it is EMPLOYEE-OUTPUT-Ryou do a WRITE EMPLOYEE-OUTPUT-RECORD, whatevers stored in EMPLOYEEis written to the file.

    In the same fashion, when you access data from DB2 Tables in a COBOLneed to first DECLARE(announce) the TABLE in the COBOL Program. SuppEMPLOYEE DB2 Table, that you want to access in the COBOL Program. ThTable looks like this -

    Before using EMPLOYEE Table, you must DECLARE the SQL EMPLOYEE TABLEthe COBOL Program in the DATA DIVISION. You can DECLARE EMPLOYEE TABthe picture below -

    http://lh5.ggpht.com/_sQvdFWqMlMg/TDkl4bF2uLI/AAAAAAAAC7Y/HMFGZ6WlSaU/s1600-h/Image20615.png
  • 8/9/2019 What Are DB2 Storage Areas

    3/9

    The DECLARE EMPLOYEE TABLE in the COBOL Program makes a statement COBOL Program uses the EMPLOYEE table, which is broken into EMPID, EJDATE SQL-Fields(columns). Since, this is SQL, and not COBOL Statemethem put them inside a EXEC SQL and END-EXEC Block.

    When you fetch data from these SQL Fields(columns) into the COBOL, talso be corresponding COBOL Variables to receive and hold the fetcheevery SQL-Field, you must declare a corresponding COBOL Variable(Sto

    http://lh5.ggpht.com/_sQvdFWqMlMg/TDkl9ilNgxI/AAAAAAAAC7o/yHsPJ-M9N-o/s1600-h/Image21215.pnghttp://lh6.ggpht.com/_sQvdFWqMlMg/TDkl7DxsFGI/AAAAAAAAC7g/DPP09wXu77I/s1600-h/Image20517.png
  • 8/9/2019 What Are DB2 Storage Areas

    4/9

    EMPLOYEE-ID is the receiving COBOL Variable which will hold the valuthe SQL Field EMPID. The SQL Type INTEGER(4 Bytes) translates toS9(09) COMP(4 Bytes) in COBOL. EMPLOYEE-NAME is the receiving COBOL shall hold the values fetched from the SQL Field ENAME. The SQL Typetranslates in COBOL as PIC X(n), where n is the size. EMPLOYEE-NAME

    COBOL variable that shall, store the data fetched from SQL Field SALType DECIMAL(7,2), 7 is the total width, 2 is the digits after the dTherefore, digits before the decimal point would be7-2=5. Thus, in COBOL it translates to PIC 9(05)V99 COMP-3. EMPLOYEEthe receiving COBOL Variable, for the values retrieved from JDATE SQGenerally, DATE in SQL becomes a PIC X(10) in COBOL, TIME is a PIC XTIMESTAMP is a PIC X(26).

    Specifying the table-name, SQL Fields and the corresponding COBOL Vatop of the COBOL Program is called Declaration. You can write DB2 Demanually, the way Ive written, or you can auto-generate it. The toogenerates Declarations, so that you can avoid the manual labour of wtop of the COBOL Program, is called Declaration Generator Tool(DCLGE

    Q. How do you use DCLGEN tool, to auto-generate Declarations?

    The Declarations Generator(DCLGEN) tool can be found inside the DB2IInteractive) Menu 2.

    I have typed the Table-name EMPLOYEE. I have also keyed in the outpu

    http://lh4.ggpht.com/_sQvdFWqMlMg/TDkmA3YnbvI/AAAAAAAAC7w/l7yB-maTO9A/s1600-h/Image20835.png
  • 8/9/2019 What Are DB2 Storage Areas

    5/9

    a member of a pds/library), in which the auto-generated DB2 Declaratby the DCLGEN Tool. After hitting ENTER, you should get a message, ECOMPLETE.

    This is how the DB2 Declarations auto-generated in the AGY0157.DEMO.file look like -

    Q. How to use Cursors to retrieve data from DB2 Tables in COBOL?

    When you fetch Data from DB2 Tables in COBOL, you cannot directly fefrom the table in one shot, in one go. Once you execute the query, tthe Query are fetched from the DB2 Table, and are brought intoanother temporary, rough-work area called Cursor Area. The Cursor rough scratch-pad area. A marker(pointer) is set to point to the begin Cursor area. Now, when you fetch the data from the cursor, it reato which the marker(pointer) is currently set. After each FETCH, theis incremented(set) to the next row. This way you FETCH data from th

    http://lh5.ggpht.com/_sQvdFWqMlMg/TDkmGEt_x5I/AAAAAAAAC8A/-0MS8uGzweo/s1600-h/Image21015.pnghttp://lh5.ggpht.com/_sQvdFWqMlMg/TDkmDtuhSqI/AAAAAAAAC74/OGQXHet3FPA/s1600-h/Image20915.png
  • 8/9/2019 What Are DB2 Storage Areas

    6/9

    area, row-by-row, until youve read all the rows, and reached the enArea.

    In this fashion in COBOL-DB2 Programs, whenever the result of a SQL multiple-rows, you must use cursors to fetch the data results row-by

    time.

    There are 4 steps to use a Cursor. First, you must DECLARE the CursoC1 CURSOR merely tells, what query to run. Second, you OPEN the cursOPEN the cursor, the cursor Query is executed, and rows are fetched Table into the Cursor Area. Third, you FETCH data from the Cursor arCOBOL Program row-by-row, until you reach the End of the Cursor. Fouyou CLOSE the Cursor.

    Whenever you perform any SQL Operation, like an OPEN cursor, FETCH dcursor, or a CLOSE cursor, the DB2 System returns back a 3-digit staindicate the Success or Failure of the SQL operation. This 3-digit sstored in a Communication Area shared between DB2 and your COBOL ProThis Shared communication-area is called SQLCA. The SQLCA is a 01-levariable(storage-area) in COBOL, that is broken down into two parts SQLCODEand SQLSTATE.

    Q. Could you show me a COBOL Program to read data-rows from a DB2 Ta

    First, you need to declare the Cursor Query. DECLARE C1 CURSOR FOR iused to declare a cursor. I am going to declare a cursor, which can Employee-id, name, salary and joining-date from the Employee-table.

    You also need to supply the SQLCA COBOL Variable, that shall hold thOPEN, FETCH and CLOSE Cursor operations. You want to be sure, that OCLOSE Cursor operations go clean, without any errors, so you needSQLthat. You generally type an INCLUDE SQLCA entry.

    Now, you begin type executable COBOL Instructions in the PROCEDURE D

    http://lh3.ggpht.com/_sQvdFWqMlMg/TDkmIwb2N1I/AAAAAAAAC8I/hNeuuFhhNA0/s1600-h/Image213%5B1%5D%5B5%5D.png
  • 8/9/2019 What Are DB2 Storage Areas

    7/9

    Essentially, the task of fetching data-rows from the DB2 Table can b3-paragraphs 1. OPENing the Cursor 2. FETCHing data from the Cursor 3. CLOSEing Cursor. I have typed the MAIN Paragraph in the PROCEDUREthis -

    Line 37 performs the Open paragraph, to open the cursor.

    When you do a FETCH in SQL, it returns(retrieves) back the row, the iscurrently pointing to. Every-time you FETCH, the current-row is recursor is incremented(set to the next row). A SQLCODE=+0 signals a sFETCH. This sequential row-after-row process of FETCHing, continueshow do you know, how many rows you want to fetch 5, 10, 15, how maprecise. You dont!

    The situation where the cursor points to the Last row, and there is get, is detected using the special SQLCODE=+100. A SQLCODE=+100 is tspecial, exceptional condition, where the cursor points to the last

    are no more data-rows to FETCH. You should do as many FETCHs until,SQLCODE=+100.

    Line 38 in the PROCEDURE DIVISION, performs FETCH paragraph to get otime, repeatedly until SQLCODE is set to +100.

    Line 39 performs the Close, to close the cursor.

    To Open a Cursor(and execute the Cursor query), in SQL you type OPENhave coded 100-OPEN Paragraph below, which executes the SQL instructEMP_CSR on Line 44. When you OPEN EMP_CSR, it means

    SELECT EMPID,ENAME,SALARY,JDATEFROM EMPLOYEE

    query will run. The results will be stored in a SQL Area EMP_CSR. the Cursor Area EMP_CSR are

    --> 1 RAM 1000 2010-06-09

    http://lh5.ggpht.com/_sQvdFWqMlMg/TDkmLcPmRFI/AAAAAAAAC8Q/uG98tl3y5EM/s1600-h/Image214%5B1%5D%5B5%5D.png
  • 8/9/2019 What Are DB2 Storage Areas

    8/9

    2 RAJ 2000 2005-01-033 RAKESH 3000 2005-02-27

    A marker or a pointer is set to the first row in the Cursor-Area. Thposition, tells whats the next row to be read?

    To fetch(receive) data-rows from the SQL Cursor-area into the COBOL you type

    FETCH cursor-area-nameINTO COBOL-variables

    The 200-FETCH Paragraph from Lines 48-56 fetches 1 single row from tEMP_CSR. So, this reads the row [1 RAM 1000 2010-06-09]. Butarrives in the COBOL Program, where is it stored? So you also supplyVariables corresponding to each SQL Data-value received. In the pictEMPLOYEE-ID COBOL Variable will receive the value 1, EMPLOYEE-NAME Cwill receive the value RAM, EMPLOYEE-SALARY COBOL Variable will rec

    1000, and EMPLOYEE-JDATE COBOL Variable will receive the value 2010-cursor-area-name is a SQL Instruction. When you want to use COBOL VaVariables) like EMPLOYEE-ID, EMPLOYEE-NAME etc. in a SQL Instructionthem with a :(Colon). I have DISPLAYed the recently fetched EMPLOYEEAfter the FETCH Operation is completed successfully, the SQLCODE is pointer is incremented and set to the next row in the cursor area.

    1 RAM 1000 2010-06-09--> 2 RAJ 2000 2005-01-03

    3 RAKESH 3000 2005-02-27

    The 200-FETCH Paragraph is performed over and over again, till youvrow, until SQLCODE=+100. The 2nd FETCH will store 2 in EMPLOYEE-ID, NAME, 2000 in EMPLOYEE-SALARY and 2005-01-03 in EMPLOYEE-JDATE.

    http://lh4.ggpht.com/_sQvdFWqMlMg/TDkmNvpc8qI/AAAAAAAAC8Y/tz5EQ7fuRj4/s1600-h/Image215%5B1%5D%5B5%5D.png
  • 8/9/2019 What Are DB2 Storage Areas

    9/9

    After youre done with retrieving/working with all the data rows in

    you must CLOSE the Cursor-area. To close the SQL Cursor-area, in SQL

    CLOSE cursor-area-name

    The 300-CLOSE Paragraph on Lines 59-63 executes the CLOSE EMP_CSR SQclose the cursor.

    You might also like:

    http://lh4.ggpht.com/_sQvdFWqMlMg/TDkmSXvNgrI/AAAAAAAAC8o/kVfB6sjr4wA/s1600-h/Image217%5B1%5D%5B5%5D.pnghttp://lh5.ggpht.com/_sQvdFWqMlMg/TDkmQXSmcOI/AAAAAAAAC8g/_9iKWoVoy3w/s1600-h/Image216%5B1%5D%5B5%5D.png