Issue in executing DB2 in C program



Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390

Issue in executing DB2 in C program

Postby Sundari_tcs » Tue Jul 02, 2013 1:00 pm

Hi all,

The Compile/link/bind/run of the C program with DB has been successfully executed and a output file has been generated. This file displays only the mentioned printf statements. In this scenario it is not showing any sqlcode too.

The same program gives a Sqlcode when the compilation fails.

For eg: I tried executing the code by calling database name. And got sql code -84 (UNACCEPTABLE SQL CODE).

sqlcode84.jpg


I am attaching respective code with this for your review. Kindly suggest me the ways to get the proper output.

#include <stdlib.h>                                       
#include <string.h>                                       
#include <stdio.h>                                       
#include <ctype.h>                                       
                                                         
EXEC SQL INCLUDE SQLCA;                                   
EXEC SQL INCLUDE SQLDA;                                   
                                                         
EXEC SQL BEGIN DECLARE SECTION;                           
char e_empdept??(3??);                                                                   
                                                           
                                   
   EXEC SQL DECLARE C1 CURSOR FOR                         
     SELECT EMPDEPT FROM tablename                     
       WHERE ID = 10;                                     
EXEC SQL END DECLARE SECTION;                                                                         
                                                           
int main(int argc,char *argv??(??))
{                                       
printf("*** BEGIN OF PROGRAM *** \n");   
                                         
                                         
   EXEC SQL OPEN C1;                     
                                         
   printf("SQL ERROR CODE: \n",SQLCODE);
                                         
   while (SQLCODE==0)                   
   {                                     
     EXEC SQL FETCH C1 INTO :e_empdept; 
   }                                     
                                         
   EXEC SQL CLOSE C1;                   
printf("The EMP DEPT IS: \n", e_empdept);
return 0;
You do not have the required permissions to view the files attached to this post.
Sundari_tcs
 
Posts: 6
Joined: Sat Jun 29, 2013 12:27 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Issue in executing DB2 in C program

Postby NicC » Tue Jul 02, 2013 5:55 pm

1 - Please do not post attachments. Some people cannot download them for company policy or other reasons, some people will not download them because they cannot be bothered or they fear a virus.
2 -
The same program gives a Sqlcode when the compilation fails.
is rubbish - if a compilation fails then the program does not execute so you cannot get an SQL code
3 - I do not understand your general post! What is displaying on a 'successful' execution? What SHOULD be displaying on successful execution?
4 - Why do you assume an error happens when you open the cursor (you have an unconditional display of SQL ERROR CODE - perhaps you mean SQL CODE FROM OPEN CURSOR?)
5 - You are doing multiple FETCHes but only display the result of the last
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Issue in executing DB2 in C program

Postby Sundari_tcs » Tue Jul 02, 2013 11:33 pm

1. Nicc @ Thanks for your suggestion regarding attachments

2. Yes i agree with u regarding the compilation,but i coming to say that when the compilation fails i am able to get the sql error code (screen shot of it was given in previous post).When the compile/bind/run was executing properly i am unable to get the sql error code.Actually i need to know where the fault occurs.

Is there any-other way to check the sql error code other than printf statement??

3.Once the code able to fetch the data from the db table then it is successfully executed.

4. I cant able able to figure out where the error occurs.Regarding this i need the help from the forum.

5.Only single fetch is done.

Kindly suggest me the way to resolve it.
Sundari_tcs
 
Posts: 6
Joined: Sat Jun 29, 2013 12:27 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Issue in executing DB2 in C program

Postby dick scherrer » Tue Jul 02, 2013 11:48 pm

Hello,
that when the compilation fails i am able to get the sql error code

If the complie has ANY problems, the execution should not run.

Fix the compile problem(s) and then run the test. Until the compile is clean, there is nothing we should work with . . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Issue in executing DB2 in C program

Postby NicC » Wed Jul 03, 2013 2:53 am

As stated before, and repeated by Dick, If the compilation fails the code cannot possibly run. If the execution step DOES run (which it should not afte a compile or link failure) then possibly it is executing a previous version of the program.
Another thing - you do not display the sqlcode after the fetch. You have no display statements within the loop so how do you know it on executes once?
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Issue in executing DB2 in C program

Postby Akatsukami » Wed Jul 03, 2013 3:14 am

Sundari_tcs wrote:The same program gives a Sqlcode when the compilation fails.

For eg: I tried executing the code by calling database name. And got sql code -84 (UNACCEPTABLE SQL CODE).

If you are compiling using the DB2 coprocessor, the erroneous SQL that your show in your attachment sqlcode84.jpg will be replaced by the two statements (SQLCODE = -84; strcpy (SQLSTATE, "42612");) that you see. The coprocessed source will then be passed to the C compiler, but with compile options preventing the generation of an object module. Thus, you are confused into thinking that you've actually compiled your source module.
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Issue in executing DB2 in C program

Postby Sundari_tcs » Sat Jul 06, 2013 11:54 am

Hi all,

Thanks for the suggestion.

Kindly please help me by verifying this code.I am not getting the output.

#include <stdlib.h>                                       
#include <string.h>                                       
#include <stdio.h>                                       
#include <ctype.h>                                       
                                                         
EXEC SQL INCLUDE SQLCA;                                   
EXEC SQL INCLUDE SQLDA;                                   
                                                         
EXEC SQL BEGIN DECLARE SECTION;                           
char e_empdept??(3??);                                                                   
                                                           
                                   
   EXEC SQL DECLARE C1 CURSOR FOR                         
     SELECT EMPDEPT FROM tablename                     
       WHERE ID = 10;                                     
EXEC SQL END DECLARE SECTION;                                                                         
                                                           
int main(int argc,char *argv??(??))
{                                       
printf("*** BEGIN OF PROGRAM *** \n");   
                                         
                                         
   EXEC SQL OPEN C1;                     
                                         
   printf("SQL ERROR CODE: \n",SQLCODE);
                                         
   while (SQLCODE==0)                   
   {                                     
     EXEC SQL FETCH C1 INTO :e_empdept; 
   }                                     
                                         
   EXEC SQL CLOSE C1;                   
printf("The EMP DEPT IS: \n", e_empdept);
return 0;
Sundari_tcs
 
Posts: 6
Joined: Sat Jun 29, 2013 12:27 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Issue in executing DB2 in C program

Postby prino » Sat Jul 06, 2013 1:48 pm

Just a thought, given by the experience of dealing with your "colleagues" off-loaded onto "that" system, shouldn't Tata Consultancy Services, the tcs in your name be giving you the training that you require to do your job?
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 635
Joined: Wed Mar 11, 2009 12:22 am
Location: Vilnius, Lithuania
Has thanked: 3 times
Been thanked: 28 times

Re: Issue in executing DB2 in C program

Postby NicC » Sat Jul 06, 2013 3:32 pm

What is the difference between this code and what you originally posted? What output are you not getting - the output you expected (and what was it that you expected?), any output at all? If you are getting output - what output are you getting?
As far as I can see you ares till doing multiple fetches but only display the last value fetched. You still display "ERROR" after the open cursor even if it gives an SQL code of 000. You do not check that the SQLCODE from the fetch is valid (100) or an error -xxx. Put some diagnostics in and run the program. Fix any error. If you cannot fix the error - and none of your colloeagues can either then post your revised code AND you error messages. It woul also be a good idea to post some of the data in tablename and what output you got.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times


Return to C, C++

 


  • Related topics
    Replies
    Views
    Last post