Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    14

    Question Unanswered: C : Undeclared host variables when included from a .h file.

    Hi,
    I have a C program handling cursors. In the EXEC SQL FETCH statement, I have two VARCHAR fields, one integer field and one float field. I have created .h files with db2dclgn for the tables and have included the same in my C program using EXEC SQL INCLUDE. The INCLUDE is placed correctly before the struct are used.

    Code:
    struct mytable1 mytable1 ;
    struct mytable2 mytable2 ;
    struct mytable3 mytable3 ;
    
    EXEC SQL FETCH C1 INTO :mytable1.varchar1, :mytable2.varchar2, :mytable3.integer1, :mytable3.float1 ;
    The pre-compiler complains for :mytable2.varchar2, :mytable3.integer1 saying it is undeclared. Firstly, I don't know why they should be undeclared ? Secondly, how come the pre-compiler treats the other host variables as legal ?
    Last edited by CogitoErgoSum; 12-30-09 at 02:25. Reason: fixed typo in code block

  2. #2
    Join Date
    Jan 2009
    Location
    Zoetermeer, Holland
    Posts
    746
    Quote Originally Posted by CogitoErgoSum View Post
    using EXEC SQL INCLUDE. The INCLUDE is placed correctly before the struct are used.
    Not sure about C. I am sure about cobol and there the SQL INCUDE should replace the struct (or, in my case, the copybook). Your compiler will include your copybooks/.h files. That is too late, the db2-precompiler-party is over by then. You should make sure that the db2-precompiler does the "including" so it knows the contents. Take a close look in the samples direcories. It should be there.

  3. #3
    Join Date
    Oct 2009
    Posts
    14
    The inclusion is indeed happening as I can see the db2 pre-compiler message related to inclusion.

    But, the troublesome thing is, it complains only for middle two columns. If inclusion is indeed the problem, then how come other host variables are not flagged as well ? The EXEC SQL are coded in a bunch for all tables.

  4. #4
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    Could you show the relevant part of the code (header + .c file) as well as the exact PRECOMPILE/PREP command and output produced by it?
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  5. #5
    Join Date
    Oct 2009
    Posts
    14
    The C code (excerpt) is shown below :
    Code:
    #include <stdio.h>
    #include <sql.h>
    #include <sqlca.h>
    #include <string.h>
    
    #include 'IC.h'
    #include 'OG.h'
    #include 'RANK.h'
    
    struct sqlca sqlca ;
    struct IC IC ;
    struct OG OG ;
    struct RANK RANK ;
    
    int program_rc = 0 ;
    int continue_process = 1 ;
    int sql_error = 0 ;
    
    EXEC SQL declare C_MSISDN cursor for
            SELECT A.V1, A.V2, B.INT1, C.FLOAT1
              FROM EXPT.IC A, EXPT.OG B, EXPT.RANK C
             WHERE .....
          ;
    .
    .
    .
    .
    .
    .
    .
    .
    void fetch_next () {
        int SQLCode = (int) sqlca.sqlcode ;
        
        EXEC SQL FETCH C_MSISDN into :IC.varchar1, :IC.varchar2, :OG.og_count, :RANK.score ;
      
        switch (SQLCode)
            {
                case 0 :
                    break ;
                case 100 :
                    printf("No more rows in cursor to process \n");
                    continue_process = 0 ;
                    break ;
                default :
                    continue_process = 0 ;
                    print_sqlerror();
            }
    }
    Pre-compile :
    Code:
    db2 prep runtest.sqc nolinemacro
    Pre-compile output :
    Code:
    LINE    MESSAGES FOR runtest.sqc
    ------  --------------------------------------------------------------------
            SQL0060W  The "C" precompiler is in progress.
       56   SQL4002N  "OG.og_count" and 
                      "IC.varchar2" are undeclared host 
                      variables that cannot both be used as descriptor names in a 
                      single SQL statement.
            SQL0092N  No package was created because of previous 
                      errors.
            SQL0091W  Precompilation or binding was ended with "2" 
                      errors and "0" warnings.

  6. #6
    Join Date
    Oct 2009
    Posts
    14
    The header files are simple. The integer field has been defined as sqlint32 and the varchar fields as structure with length and data fields.

    Here is one more thing. I tried with a DECLARE SECTION. Pre-compile, compile and link all go fine. But, when I run the code, at the FETCH statement, I get a segmentation fault.

  7. #7
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    There are quite a few basic problems:
    (1) #include '...' won't work/compile because a single-quote in C/C++ denotes a character and not a string (this is different in SQL)
    (2) You have no DECLARE SECTION, so the precompiler cannot find any host variables.
    (3) Use
    Code:
    EXEC SQL INCLUDE OG.h;
    instead of #include "OG.h" so that the precompiler is aware of this.
    (4) Don't use casts on the SQLCODE - use it directly to avoid potential problems arising from the casting. (As a general rule: if you need a cast, you typically have a design problem in your code.)
    (5) You should treat positive SQL codes differently than negative ones because positive codes denote warnings only and are not error conditions.
    (6) I always recommend to do this:
    Code:
    EXEC SQL INCLUDE SQLCA;
    instead of #include <sqlca.h>
    (7) I also recommend to do this:
    Code:
    EXEC SQL DECLARE SQLCA;
    instead of "struct sqlca sqlca;" and place this inside each function or method that contains embedded SQL statements. Otherwise, you have a global variable and that impacts reentrancy most severely.


    Regarding the segfaults, it would be usual debugging that can be applied to find the reason for this. But I think there are some basic problems with the code (see above), which suggests that other problems can be present in the pieces not shown here. Maybe it is something simple like trying to access a pointer that was not properly initialized? Hard to guess...
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •