Wednesday 4 September 2013

DB2 Cursor | Cursors in DB2 | DB2 Cursor Type | SQL Cursors | COBOL+DB2 application Cursors.

Cursors in DB2, DB2 Cursors, Cursors Type
Cursors in DB2



Have you ever heard about the term DB2 Cursor? and what is the use of Cursor in application programming. In this article, you'll learn everything about DB2 cursors or SQL cursors. In general, SQL cursors are used to retrieve data (i.e. row by row) from a result set. SQL cursors are used heavily in COBOL+Db2 Applications for row-by-row data processing. Let's get started with DB2 Cursor Tutorial.


Agenda. 
  • Introduction.
  • What is Cursor in DB2?
  • What is the use of Cursor in Cobol?
  • How does the cursor work in Db2?
  • Conclusion.

Introduction.

DB2 is a relational database. It's widely used in an enterprise application to store customer or transactional data. The stored data is accessed via different front-end applications (i.e. COBOL+DB2 applications.) as per business requirements.

 But there is a significant difference between the SQL and host languages such as COBOL, JAVA, C++ processing. COBOL is designed to process a single record at a time. However, the SQL process a set of records at a time. 

To solve this problem IBM introduces the concept of DB2 Cursors or Cursors in DB2 or SQL Cursors.

What is Cursor in DB2?

A cursor in DB2 is a mechanism by which you access data row-by-row from the result set. Cursors in DB2 keep track of records in the results set, and with the help of cursors, you can update, delete, or select individual rows from the table. 

You can fetch data from the DB2 table via SQL statement, then why do you need a cursor in COBOL+DB2 applications?

What is the use of Cursor in COBOL+DB2 Application?

You can access data from the DB2 table by using singleton SELECT statements and they are very efficient as compare to DB2 cursors. But, the application program will fail (DB2 Error code: -811) if the singleton SELECT statement returns multiple rows from the result set.



COBOL or any other programming language is designed to process single records at a time. However, SQL is capable of the process a set of records at times.  

Thus, you need a proper mechanism to process data in your COBOL+DB2 Application. COBOL+DB2 application uses a cursor to navigate through a set of records (i.e. result set) created by embedded SQL SELECT statements. 

DB2 Cursors Types. 

Cursors in DB2 were broadly categorised into the following two categories. 

Cursors in DB2, DB2 Cursors, COBOL+DB2 Applications
Cursors Type.

  • Scrollable Cursor: DB2 Scrollable Cursor provide the capability to scroll forward and backwards through data (i.e. result set) in an application program without any additional code. COBOL+DB2 application can easily navigate forward or backwards as per business requirements. The SCROLL keyword is used to specify a cursor as a scrollable cursor.
  • Serial Cursor: DB2 Serial Cursor is a basic variant of DB2 cursors. In this case, data can only be accessed in a forward direction.


How does the cursor work in Db2?

The cursor is similar to a pointer. As the programmer, you declare the cursor and specify its SQL statement so that it works. The SELECT statements must be enclosed in a DECLARE CURSOR statement. 

In the DECLARE CURSOR statement, we give the cursor a name and identify the set of rows to retrieve with the SELECT statement. This set of rows is referred to as the result table. After that, the cursor basically functions like a sequential file. It opens, rows are pulled one by one from the cursor, and then the DB2 cursor is closed. 

Cursors support four functions:
  • DECLARE Cursor: Declare a cursor and identify the rows you wish to access with it.
  • OPEN Statement: An OPEN statement will inform DB2 that you're ready to process the first row of the result table.
  • FETCH Statement: Each row of the results table is returned by the FETCH statement and assigned to specific host variables.
  • Close Statement: As a final step, the CLOSE statement closes the cursor, which releases all resources.

Youtube Tutorial: DB2 Cursor | DB2 Cursor Types | COBOL+DB2 Cursor. 

Conclusion. 

Finally, this marks an end to the Cursors in DB2 or SQL Cursor or DB2 Cursors. This article provides an overview of DB2 Cursors and the type of Cursors in DB2. It also outlines the different stages of DB2 cursors. Do check out COBOL LEVEL 88 Condition.


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™

Created with Artisteer

1 comment:

  1. Check out the mainframe blog
    http://mframes.blogspot.com

    ReplyDelete

New In-feed ads