SkyronWtr
SkyronWtr

Reputation: 13

Reading and executing sql statements in cobol

First of all, sorry if this has allready been asked, I thoroughly searched, but can't seem to find an answer.

This is a project for school, we do realise this could be solved in a much more efficient way, but that is not an option.

So here is the problem:

  1. We have to use Microsoft SQL server 2008 R2, here we have an empty database.
  2. Using Cobol, we have to create all tables, using a parameter file sqlstatements.sql (a.i. read every statement, execute them, and so on.

Our problem we've encountered (and which even our teachers can't solve atm) is that:

  1. the sql statements are being read in a very strange fashion (statements are being scrambled randomly)
  2. we don't seem to have any rights to actually edit the database.

Here is the cobol code:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. DB_connection.

   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
    SELECT sqlscript assign to "databaseScript.sql"
    ORGANIZATION IS LINE SEQUENTIAL.
    SELECT logfile assign to "logfile.txt"
    ORGANIZATION IS LINE SEQUENTIAL.
   DATA DIVISION.
   FILE SECTION.
   FD sqlscript.
   01 sqlstatement.
   FD logfile.
   01 logrecord.
   03 logline pic x(100).
   WORKING-STORAGE SECTION.
   EXEC SQL 
     BEGIN DECLARE SECTION
   END-EXEC

  * SQLCODE is 0 for success, 100 for no data, -1 for failure
   01 SQLCODE PIC S9(3). 

  * SQLSTATE is a 5 character communication code; 00xxx is success.
   01 SQLSTATE PIC X(5). 

   01 JdbcString PIC X(255).

   EXEC SQL 
     END DECLARE SECTION
   END-EXEC
   01 einde         pic X value '0'.
    88 einde-bestand value '1'. 

   PROCEDURE DIVISION.
   MAIN-PARAGRAPH.
  * Initial code
   OPEN INPUT sqlscript
   OPEN EXTEND logfile
   PERFORM DO-CONNECT

   DISPLAY "After connecting to the database:"

   DISPLAY "SQLCODE= " + SQLCODE.
   DISPLAY "SQLSTATE= " + SQLSTATE
   DISPLAY "Connectie met java succes."

   IF sqlcode = 0
   MOVE "Connectie succesvol" to logline
   WRITE logrecord
   DISPLAY "Writing to logfile"
   END-IF


   PERFORM LEESSQLBESTAND
  * lees sql bestand, read entry'sz
  * Use the database       
   CLOSE sqlscript 
   CLOSE logfile
   PERFORM DO-DISCONNECT
   ACCEPT SQLSTATE
  * Terminate the program    
   GOBACK

  * The SQL connect statement must be completed with the information
  * appropriate to the actual JDBC driver in use.  JDBC stands for
  * Java DataBase Connectivity, and it is the method by which PERCobol
  * accesses databases and database-like data sources.
  *
  * The JDBC driver itself must be included in the Java library path
  * in order to successfully connect to the database.  The JDBC driver
  * is generally included with the database itself; see the database
  * documentation for more details.
  *
  * When connecting to a datasource, the jdbc:url may be 
  * ds:data-source-name.
  *
  * jdbc:url        The JDBC url to the database itself     
  * com.driver.name This is the classname of the driver
  *
   DO-CONNECT.

   STRING "jdbc:sqlserver://localhost\SQLEXPRESS;" 
     DELIMITED BY SIZE
     "databaseName=ProjectManagement;"
     DELIMITED BY SIZE
     "userName=admin;password=broforce"
     DELIMITED BY SIZE
     INTO JdbcString

   EXEC SQL
    CONNECT
      TO :JdbcString         
      DRIVER "com.microsoft.sqlserver.jdbc.SQLServerDriver"                 
   END-EXEC.

   LEESSQLBESTAND.
   READ sqlscript
    AT END DISPLAY 'LEEG SQL BESTAND' MOVE '1' TO einde
    MOVE "Leeg SQL-Bestand" to logline
    NOT AT END DISPLAY 'MINSTENS 1 SQL STATEMENT'
    MOVE "Queries found..." to logline
   END-READ
   WRITE logrecord

   PERFORM UNTIL einde = 1
           MOVE sqlstatement to logline
           EXEC SQL
                   EXECUTE sqlstatement
           END-EXEC
           IF sqlcode < 0
           move "failed" to logline
           WRITE logrecord
           END-IF
           IF sqlcode = 0
           move "succes" to logline
           WRITE logrecord
           END-IF

           READ sqlscript
             AT END DISPLAY 'EINDE SQL BESTAND'
             MOVE '1' TO einde
             MOVE "Einde Bestand" to logline
           END-READ
           WRITE logrecord
   EXEC SQL COMMIT WORK END-EXEC       
   END-PERFORM


  * Disconnect from the SQL database connection.  This allows the
  * JDBC driver to free any resources required for the connection.


   DO-DISCONNECT.
   EXEC SQL
    DISCONNECT
   END-EXEC.

Am I doing something wrong here? We're at a loss.

Upvotes: 0

Views: 3848

Answers (2)

Kevin McCravy
Kevin McCravy

Reputation: 36

COBOL is geared towards fixed length records but it can easily handle variable length records.

...
FD sqlscript
   record is varying in size
   depending on sqlstatement-length.
01 sqlstatement.
   05 filler pic x occurs 0 to 9999 times
      depending on sqlstatement-length.
...
working-storage
...
01 sqlstatement-length pic 9(4) binary.
...
procedure division.
...
read sqlscript
*then you can use sqlstatement-length to grab what you need
*you might want to add if sqlstatement-length = zero logic for blank lines
move sqlstatement(1:sqlstatement-length) to wherever

Upvotes: 1

Marcus_33
Marcus_33

Reputation: 464

Random scrambling, assuming I'm interpreting what that means correctly, generally means you either need to initialize your variables that you're reading in to (leftover data junking up your working storage) or that you're moving data between incompatible types or wrong-sized fields. Since you have no picture clause on your sqlstatement (how does that even work?) it's hard to tell what's wrong there.

As far as permissions go, I'm only experienced in an IBM/DB2 environment but I always have to get permissions set for my individual program in DB2 itself to make it allowed to modify my bases.

Upvotes: 0

Related Questions