Page 1 of 1

Issue in executing DB2 in C program

PostPosted: Tue Jul 02, 2013 1:00 pm
by Sundari_tcs
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;

Re: Issue in executing DB2 in C program

PostPosted: Tue Jul 02, 2013 5:55 pm
by NicC
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

Re: Issue in executing DB2 in C program

PostPosted: Tue Jul 02, 2013 11:33 pm
by Sundari_tcs
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.

Re: Issue in executing DB2 in C program

PostPosted: Tue Jul 02, 2013 11:48 pm
by dick scherrer
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 . . .

Re: Issue in executing DB2 in C program

PostPosted: Wed Jul 03, 2013 2:53 am
by NicC
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?

Re: Issue in executing DB2 in C program

PostPosted: Wed Jul 03, 2013 3:14 am
by Akatsukami
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.

Re: Issue in executing DB2 in C program

PostPosted: Sat Jul 06, 2013 11:54 am
by Sundari_tcs
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;

Re: Issue in executing DB2 in C program

PostPosted: Sat Jul 06, 2013 1:48 pm
by prino
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?

Re: Issue in executing DB2 in C program

PostPosted: Sat Jul 06, 2013 3:32 pm
by NicC
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.