ex17.c

Example for selecting from a refcursor.

00001 /* $Id: ex17.c 221 2002-08-24 12:54:47Z kpoitschke $ */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include "examples.h"
00005 
00006 int select_refcursor(sqlo_db_handle_t dbh, double min_salary)
00007 {
00008   sqlo_stmt_handle_t sth = SQLO_STH_INIT;
00009   sqlo_stmt_handle_t st2h;                     /* handle of the ref cursor */
00010   double sal = min_salary;
00011   CONST char **v;
00012   int status;
00013 
00014   CONST char * stmt = 
00015     "BEGIN\n"
00016     "    OPEN :c1 FOR SELECT ENAME, SAL FROM EMP WHERE SAL >= :min_sal ORDER BY 2,1;\n"
00017     "END;\n";
00018 
00019 
00020   /* parse the statement */
00021   if ( 0 <= (sth = sqlo_prepare(dbh, stmt))) {
00022     /* bind all variables */
00023     if (SQLO_SUCCESS != 
00024         ( sqlo_bind_ref_cursor(sth, ":c1", &st2h)) ||
00025         (sqlo_bind_by_name(sth, ":min_sal", SQLOT_FLT, &sal, sizeof(sal), 0, 0) ) ) {
00026       error_exit(dbh, "sqlo_bind_by_name");
00027     } else {
00028 
00029       /* execute the PL/SQL block */
00030       if (SQLO_SUCCESS != sqlo_execute(sth, 1))
00031         error_exit(dbh, "sqlo_execute");
00032     }
00033     /* execute the refcursor */
00034     if (SQLO_SUCCESS != sqlo_execute(st2h, 1)) {
00035       error_exit(dbh, "sqlo_execute(ref)");
00036     }
00037 
00038     while (SQLO_SUCCESS == (status = sqlo_fetch(st2h, 1))) {
00039       v = sqlo_values(st2h, NULL, 1);
00040 
00041       printf("Name=%-8s Salary= %-6s\n", v[0], v[1]);
00042     }
00043     
00044     if (status != SQLO_NO_DATA)
00045       error_exit(dbh, "sqlo_fetch(st2)");
00046 
00047     if (SQLO_SUCCESS != sqlo_close(sth))
00048       error_exit(dbh, "sqlo_close(1)");
00049 
00050     if (SQLO_SUCCESS != sqlo_close(st2h))
00051       error_exit(dbh, "sqlo_close(2)");
00052 
00053   } else {
00054     error_exit(dbh, "sqlo_prepare");
00055   }
00056 
00057   return 1;
00058 }
00059 
00060 /* $Id: ex17.c 221 2002-08-24 12:54:47Z kpoitschke $ */

Generated on Mon May 21 13:43:28 2007 for libsqlora8 by  doxygen 1.4.7