exec sql include sqlda;
sqlda *bind_dp;
sqlda *select_dp;
char sql_start[100];void main()
{
exec sql prepare s from 'SELECT A,B,C FROM TEMP';
exec sql declare c cursor for s;
exec sql open c user descriptor bind_dp;
exec sql close c;
for(i=0;i<max_items;i++){
if (bind_dp->v[i]!=(char *) 0)
.....
}
}
exec sql declare cursor cur_abc

解决方案 »

  1.   

    EXEC SQL INCLUDE sqlca;/* global variables */
    char    dyn_statement[1024];                      /* statement variable     */
    EXEC SQL VAR dyn_statement IS STRING(1024);
    char *dml_commands[] = {"SELECT", "select", "INSERT", "insert",
                            "UPDATE", "update", "DELETE", "delete"};int select_found, cursor_open = 0;
    void main()
    {    /* Connect to the database. */
        if (oracle_connect() != 0)
            exit(1);    EXEC SQL WHENEVER SQLERROR DO sql_error();    /* Allocate the input and output descriptors. */
        EXEC SQL FOR :max_array_size
                 ALLOCATE DESCRIPTOR GLOBAL :indesc;
        EXEC SQL FOR :max_array_size
                 ALLOCATE DESCRIPTOR GLOBAL :outdesc;    /* Process SQL statements. */
        for (;;) 
        {
            (void) setjmp(jmp_continue);        /* Get the statement.  Break on "exit". */
            if (get_dyn_statement() != 0)
                break;        /* Prepare the statement and declare a cursor. */
            parse_flag = 1;     /* Set a flag for sql_error(). */
            EXEC SQL PREPARE S FROM :dyn_statement;
            parse_flag = 0;     /* Unset the flag. */        EXEC SQL DECLARE C CURSOR FOR S;        /* Call the function that processes the input. */
            if (process_input())
                exit(1);        /* Open the cursor and execute the statement. */
            EXEC SQL FOR :in_array_size
                OPEN C USING DESCRIPTOR GLOBAL :indesc;
            cursor_open = 1;
     
            /* Call the function that processes the output. */
            if (process_output())
                exit(1);        /* Tell user how many rows were processed. */
            rows_processed();    }   /* end of for(;;) statement-processing loop */ 
        /* Close the cursor. */
        if (cursor_open)
          EXEC SQL CLOSE C;    /* Deallocate the descriptors */
        EXEC SQL DEALLOCATE DESCRIPTOR GLOBAL :indesc;
        EXEC SQL DEALLOCATE DESCRIPTOR GLOBAL :outdesc;    EXEC SQL WHENEVER SQLERROR CONTINUE;
        EXEC SQL COMMIT WORK RELEASE;
        puts("\nHave a good day!\n");    EXEC SQL WHENEVER SQLERROR DO sql_error();
        return;
    }int get_dyn_statement()
    {
        char *cp, linebuf[256];
        int iter, plsql;    for (plsql = 0, iter = 1; ;)
        {
            if (iter == 1)
            {
                printf("\nSQL> ");
                dyn_statement[0] = '\0';
                select_found = 0;
            }
            
            fgets(linebuf, sizeof linebuf, stdin);        cp = strrchr(linebuf, '\n');
            if (cp && cp != linebuf)
                *cp = ' ';
            else if (cp == linebuf)
                continue;        if ((strncmp(linebuf, "SELECT", 6) == 0) ||
                (strncmp(linebuf, "select", 6) == 0))
            {
                select_found=1;;
            }        if ((strncmp(linebuf, "EXIT", 4) == 0) ||
                (strncmp(linebuf, "exit", 4) == 0))
            {
                return -1;
            }        else if (linebuf[0] == '?' ||
                (strncmp(linebuf, "HELP", 4) == 0) ||
                (strncmp(linebuf, "help", 4) == 0))
            {
                help();
                iter = 1;
                continue;
            }        if (strstr(linebuf, "BEGIN") ||
                (strstr(linebuf, "begin")))
            {
                plsql = 1;
            }        strcat(dyn_statement, linebuf);        if ((plsql && (cp = strrchr(dyn_statement, '/'))) ||
                (!plsql && (cp = strrchr(dyn_statement, ';'))))
            {
                *cp = '\0';
                break;
            }
            else
            {
                iter++;
                printf("%3d  ", iter);
            }
        }
        return 0;
    }
    int process_input()
    {
        int i, j;
        char name[31];
        int  input_count, input_len= MAX_VAR_LEN;
        int  occurs, string_type = 5;
        int  string_len;
        char arr_size[3];    EXEC SQL DESCRIBE INPUT S USING DESCRIPTOR GLOBAL :indesc;
        EXEC SQL GET DESCRIPTOR GLOBAL :indesc :input_count = COUNT;     if (input_count > 0 && !select_found )
           {     /* get input array size */
              printf ("\nEnter value for input array size (max is %d) :  ", 
                               max_array_size);
            fgets(arr_size, 4, stdin);
            in_array_size = atoi(arr_size); 
           }
        else
           { 
             in_array_size = 1;
           }
        for (i=0; i < input_count; i++)
        {
            occurs = i +1;                       /* occurence is 1 based */
            EXEC SQL GET DESCRIPTOR GLOBAL :indesc 
                     VALUE :occurs :name = NAME;        for (j=0; j < in_array_size; j++)
            {
              if (in_array_size == 1)
                printf ("\nEnter value for input variable %*.*s:  ",10,31, name);
              else 
                printf ("\nEnter %d%s value for input variable %*.*s:  ",
                   j +1, ((j==0) ?  "st" :  (j==1) ? "nd" : (j==2) ? "rd" :"th"),
                          10,31, name);
              fgets(input[i][j], sizeof(input[i][j]), stdin);
              string_len = strlen(input[i][j]);
              input[i][j][string_len - 1 ] = '\0';   /* change \n to \0 */
            }
            EXEC SQL SET DESCRIPTOR GLOBAL :indesc
                     VALUE :occurs TYPE = :string_type, LENGTH = :input_len;
            EXEC SQL FOR :in_array_size
                     SET DESCRIPTOR GLOBAL :indesc
                         VALUE :occurs  REF DATA = :input[i];
        }    return(sqlca.sqlcode);
    }