2. Embedded SQL for C : Advanced Processing : Sample Programs : Dynamic SQL Handler
 
Share this page                  
Dynamic SQL Handler
This example shows how to declare and use a data handler in a dynamic SQL program, using the SQLDA. It uses the data handler Get_Handler() and the HDLR_PARAM structure described in the previous example.
main()
{
    exec sql include sqlda;
    /* Declare the SQLDA and IISQLHDLR structures */
    IISQLDA _sqlda;
    IISQLDA *sqlda = &_sqlda;
    IISQLHDLR datahdlr_struct;
    /*
    ** Do not declare the datahandlers or the
    ** datahandler argument to the ESQL preprocessor
    */
    int Get_Handler();
    HDLR_PARAM hdlr_arg;
    int base_type;
    int col_num;
    /* Declare null indicator to ESQL */
    exec sql begin declare section;
        short indvar;
        char stmt_buf[100];
    exec sql end declare section;
    . . .
    /*
    ** Set the IISQLHDLR structure with the appropriate
    ** datahandler and datahandler argument.
    */
    datahdlr_struct.sqlarg = &hdlr_arg;
    datahdlr_struct.sqlhdlr = Get_Handler;
    sqlda->sqln = IISQ_MAX_COLS;
    /* Describe the statement into the SQLDA */
    strcpy(stmt_buf, "select * from book");
    exec sql prepare stmt from :stmt_buf;
    exec sql describe stmt into sqlda;
    . . .
    /*
    ** Determine the base_type of the SQLDATA
    ** variables
    */
    for (col_num = 0; col_num < sqlda->sqln;
         col_num++)
    {
        base_type = abs(sqlda-
            >sqlvar[col_num].sqltype);
        /*
        ** Set the sqltype, sqldata and sqlind for
        ** each column. The long varchar column
        ** chapter text will be set to use a
        ** datahandler
        */
        if (base_type == IISQ_LVCH_TYPE)
        {
           sqlda->sqlvar[col_num].sqltype =-IISQ_HDLR_TYPE
           sqlda->sqlvar[col_num].sqldata =
                           (char *)&datahdlr_struct;
           sqlda->sqlvar[col_num].sqlind = &indvar;
        }
        Else
           . . .
    }
    /*
    ** The datahandler (Get_Handler) will be
    ** invoked for each non null value of column
    ** chapter_text retrieved. For null values
    ** the indicator variable will be set to
    ** "-1" and the datahandler will not be called
    */
    . .
    exec sql execute immediate :stmt_buf using :sqlda;
    exec sql begin;
        process rows...
    exec sql end;
    . . .