Declaring a cursor to execute a stored procedure

You can declare cursors to execute a stored procedure that in turn executes a single select statement. You create this style of cursor by calling ct_cursor with type as CS_CURSOR_DECLARE and text as an execute statement that invokes the procedure.

For example, the select statement in the example above could be invoked by a stored procedure:

CREATE PROCEDURE titlecursorproc
  @sales_val INT
 AS
  SELECT title_id, price, title FROM titles 
  WHERE ( total_sales > @sales_val ) 
  FOR READ ONLY

For Client-Library cursors that execute an Adaptive Server Enterprise stored procedure, you do not use host language variables and do not define any variable formats with ct_param—the server determines parameter formats from the declaration of the stored procedure. The steps required to declare and open the cursor are otherwise similar to those illustrated under “Declaring a cursor that takes parameters”. The example below shows how to declare and open a Client-Library cursor on the titlecursorproc stored procedure:

CS_CHAR     body[1024];
 CS_DATAFMT  intfmt;
 CS_INT      sales_val;
strcpy(body, “EXECUTE titlecursorproc”);
 ret = ct_cursor(cmd, CS_CURSOR_DECLARE, 
          “a cursor”, CS_NULLTERM, 
          body, CS_NULLTERM, CS_UNUSED);
 ... error checking deleted ...
ret = ct_cursor(cmd, CS_CURSOR_OPEN, NULL,
               CS_UNUSED, NULL, CS_UNUSED,
               CS_UNUSED);
 ... error checking deleted ...
/*
 ** Supply a value for the @sales_val parameter for
 ** titlecursorproc. 
 */
 (CS_VOID)memset(&intfmt, 0,  sizeof(intfmt));
 intfmt.datatype = CS_INT_TYPE;
 intfmt.maxlength = CS_SIZEOF(CS_INT);
 intfmt.status = CS_INPUTVALUE;
sales_val = 1;
 ret = ct_param(cmd, &intfmt, 
              (CS_VOID *)&sales_val, CS_UNUSED, 0);
 ... error checking deleted ...
/*
 ** Send the batched cursor declare and open
 ** commands.
 */
 ret = ct_send(cmd);
 ... error checking deleted ...
... results processing deleted ...