views:

560

answers:

6

I'm starting to learn about COBOL. I have some experience writing programs that deal with SQL databases and I guess I'm confused how COBOL stores and retrieves data that is stored in a mainframe for example. I know that it's not like relational databases but every example program I've seen takes data straight from the command line and I know that's not how real world COBOL programs process the data. Can someone explain or show me a good resource that can explain it?

+1  A: 

Traditional COBOL batch environments use a 'data section' of the cobol program to directly declare database connections, which are in turn set up in JCL. Since COBOL predates SQL, those would have tended to be various other types of databases, but it's likely that IBM made SQL work with DB/2. I imagine you'll get another answer from someone closer to this stuff. If you look at the SQL preprocessors available for use with other languages you'll get the idea -- a cursor becomes a native datatype and delivers query results to native variables.

bmargulies
ah. So my understanding might be slightly skewed. COBOL is more about the manipulation of the data instead and doesn't do much with actually retrieving the data. The main reason why I asked this question is i'm going into an internship this summer and I'm not doing cobol there but that is what they use to process their financial data and I was confused how you would get the data into cobol to process. I really like your answer though thank you!
controlfreak123
@bmargulies: -1: assumes IBM
John Saunders
A: 

I had to learn cobol in college (sadly), its horrid... RUN AWAY.

To answer your question, http://www.cobug.com/cobug/docs/dataaccess0037.html from a quick google search of "COBOL data access" (sans quotes)

Other tools: http://infogoal.com/cbd/cbdtol.htm

Lucas
I don't like the cobug site. Every google search I tried so far has one result on the cobug site and its all ads for software. There is almost nothing but advertising on that site.
controlfreak123
+6  A: 

COBOL is just another third generation computer language. It is just a little older than most which doesn't mean it is somehow incomplete (actually it comes with quite a bit of baggage - but that is another story).

As with any other third generation language, COBOL manipulates data files in pretty much the same way that you would in a C program. Nothing odd, mysterious or magical about it. Files are opened, read, written and closed using the file I/O features of the language.

Various mechanisms are used to form a link between an actual file and the program. The details here are often specific to the operating system you are working under. Generally, COBOL implementations try to isolate themselves from the operating environment through a logical file name as opposed to an actual name. This added indirection is improtant if you are writting programs that will be ported to different platforms (eg. write and test within an IDE on a Windows platform then run on a mainframe).

The following examples relate to an IBM Mainframe environment.

Within the IBM mainframe world, you will find that programs are run as either batch or on-line (eg. CICS). I will not describe how to set up for file I/O under CICS (long story). Programs that are used to manipulate files are usually batch. Here is a rough illustration of how a batch program works:

  1. Batch programs are run via JCL. JCL is used to identify the program to run ('EXEC' statement) and to identify which files your program will reference using 'DD' statements. The function of a DD Statement is to form a logical connection between an actual file and a name your COBOL program will reference when it wants to refer to the file (this is the isolation mechanism mentioned earlier). For example:
    JCLDDNAM DD DSN='HLQ.MY.FILE'...
    
    would associate the 'DD' name 'JCLDDNAM' to the file named 'HLQ.MY.FILE'. This part is platform dependant so the details are specific to the operating environment.
  2. In the 'FILE-CONTROL' section of your COBOL program you connect the 'DD NAME' defined in your JCL with the name you will use on each I/O statement to reference that file. This connection is defined using the 'SELECT' statement. For example:
    
    SELECT MYFILE
    ASSIGN JCLDDNAM
    remainder of select...
    makes a connection between whatever file you associated with 'JCLDDNAM' in your 'JCL' to 'MYFILE' that you will later reference in COBOL I/O statements. The SELECT statement itself is part of the ISO COBOL standard, however, many COBOL implementations define some non-standard extentions to facilitate various quirks to their file subsystems.
  3. Open, read, write, close files within the 'PROCEDURE DIVISION' of you program using the name 'MYFILE' as in:

    OPEN MYFILE
    READ MYFILE
    CLOSE MYFILE

The above is highly simplified and there are a multitude of ways to do this within COBOL. Understanding the complete picture will take some real effort, time and practice. The I/O statements illustrated above are part of the COBOL standard but every vendor will have their own extentions.

IBM COBOL supports a wide range of file organizations and access methods. You can review the IBM Enterprise COBOL Language Reference manual here to get the syntax and rules for file manipulation, However, the User Guide provides a lot of good examples for reading/writing files (you will have to dig a bit - but it is all laid out for you).

The setup to reference an SQL database via a COBOL program is somewhat different but involves setting up a connection between your program and the database subsystem. Within the IBM world this is done throug JCL, other environments will use different mechanisms.

IBM COBOL uses a pre-processor or co-processor to integrate database access and data exchange. For example, the following code would retrieve some data from a DB2 database:

MOVE 1234 TO PERSON-ID
EXEC SQL
  SELECT  FIRST_NAME,  LAST_NAME
  INTO   :FIRST-NAME, :LAST-NAME
  FROM PERSON
  WHERE PERSON_ID = :PERSON-ID
END-EXEC
DISPLAY PERSON-ID FIRST-NAME LAST-NAME

The stuff between EXEC SQL and END-EXEC is a pretty simple SQL select statement. The names preceded by colons are COBOL host variables used to pass data to DB2 or receive it back. If you have ever coded database access routines before this should look very familiar to you. This link provides a simple introduction to incorporating SQL statements into an IBM Enterpirse COBOL program.

By the way, IBM Enterprise COBOL is capable of working with XML documents too. Sorry for the heavy IBM slant, but that is the environment I am most familiar with.

Hope this gets you started in the right direction.

NealB
you pretty much hit in on the head man. Thank you for taking the time to write so much!
controlfreak123
@NealB: -1: assumes that COBOL is IBM-only.
John Saunders
@John Saunders: Point well taken - The first two paragraphs are true of all COBOL. The examples I gave were IBM specific because I am familiar with it (and I think it also represents a significant portion of the market). I did not intend to leave the impression that IBM is the only COBOL. Other COBOL implementations may differ in some of the details but the general statement that COBOL reads/writes files and accesses SQL databases in a manner similar to other 3rd generation languages is the point I was really trying to make.
NealB
@NealB: I'll be glad to remove the downvote if you edit your answer to include what you said in the comment.
John Saunders
@John Saunders: Done.
NealB
A: 

All the IBM mainframe shop's I've worked in have used COBOL which talked with a relational database. Generally that has been IBM's DB2. Please note that DB2 is a relational database that run's on mainframes. It can also be run on Windows or Linux.

Twenty years ago a predominate way to enter data into the DB2 mainframe database was to use CICS. CICS is "presentation level" software that comminicates with character based data entry screens. Consider CICS the functional equivelant of PHP or ASP.NET.

Today there are many more options to get data into DB2. CICS is still an option but your "presentation layer" could be PHP, ASP.NET, Win Forms, Java JSF, Powerbuilder. The key thing is that your development platform would need to be able to work with a DB2 database driver. The platform could be Windows, Linux, and possibly others.

My point is that data can get into the mainframe DB2 database in a many ways from many platforms. The COBOL language might be involved in data entry, reporting, altering data COBOL, etc. But it might only be part of an multiple tier application that could be part Windows, web, and mainframe . I could give specific examples if you have more information about the application you'll be working with at your internship.

Chuck
+1  A: 

Who said you cannot use SQL to retrieve data from a cobol application, maybe without spending money?

A company I used to work for, did just that - with SQLite. This little gem of a public domain library compiles SQL statements to bytecode, then it executes them.

By replacing the "backend" level of SQLite with a custom interface to the C library that deals with Cobol files, it was possible to query the Cobol data from other languages, Python in that case. It worked -- within the limits of SQLite of course, but it was stable, it seemed relational enough and it didn't even require a DB server :-)

Marco Mariani
+1  A: 

Mainframe Cobol uses embedded SQL (kinda like SQLj), e.g.:

Procedure Division.

   Exec SQL
        Select col1, col2
          from myTable
        into :ws-col1, :ws-col2
        where col0 = :col0
   End-Exec

In this case, the host variables ws-col0, ws-col1 and ws-col2 are defined in the working-storage section. The database interface manages getting that data in the right place.

Very easy compared to the distributed stuff actually.

Joe Zitzelberger
I like your answer as well. The place where I'm interning right now uses cobol for their mainframe stuff and i've been exposed to a little of it along with the CICS that they use to connect the data to our java ee apps.
controlfreak123