Scope of host variables

When host variables remain bound from one execution to the next, you must ensure that they remain in scope. Particular care must be taken when automatic variables such as stack variables are used.

When a possibly problematic situation can be detected by the precompiler, a warning is issued. Whether a host variable remains in scope or not will also depend on the overall program logic.

For example:

      /*
       ** a function called by main() 
       */
     CS_VOID insert(insert_row)
     exec sql begin declare section;
     int insert_row;/* row will go out of scope once exit
                     ** function*/
     exec sql end declare section;
     {
        /* 
            ** id is a stack variable which will go out of scope
            ** once we exit the function insert()
            ** it is not likely to be at the same address at the
            ** next call to this function, so if it is bound as
            ** an input variable, there will be errors.
            */
        exec sql begin declare section;
        int id;
        exec sql end declare section;
        exec sql insert values(:row,:id);
     }
     int fetched_row;/* this variable can be safely bound with
                      ** persistence */
     main()
     {
        exec sql begin declare section;
        /*
            ** This variable will go out of scope when the program
            ** exits main, which is not a problem.
            */
        int row;
        /*
            ** This variable is a pointer, thus it does not
            ** necessarily pose problems, depending on the scope
            ** of the data it is pointing to.
            */
        char *pointer;
        exec sql end declare section;
        for (row = 0; row < 10; row++)
        {
             insert(row);
        }
     }