ct_fetch

Description

Fetches result data.

Syntax

CS_RETCODE  ct_fetch(command, type, offset, option,
                      rows_read);
CS_COMMAND  *command;
CS_INT      type;
CS_INT      offset;
CS_INT      option;
CS_INT      *rows_read;

Parameters

command

(I) Handle for this client/server operation. This command handle must already be allocated with ct_cmd_alloc.

type

(I) This argument is currently unused and should be passed as CS_UNUSED in order to ensure compatibility with future versions of Client-Library.

offset

(I) This argument is currently unused and should be passed as CS_UNUSED in order to ensure compatibility with future versions of Client-Library.

option

(I) This argument is currently unused and should be passed as CS_UNUSED in order to ensure compatibility with future versions of Client-Library.

rows_read

(I) Variable where the number of result rows is returned. This variable is of type integer. ct_fetch sets rows_read to the number of rows read by the ct_fetch call. This argument is required.

Returns

ct_fetch returns one of the following values listed in Table 3-8.

Table 3-8: return values

Value

Meaning

CS_SUCCEED (-1)

The routine completed successfully.

ct_fetch places the total number of rows read in rows_read.

CS_FAIL (-2)

The routine failed.

ct_fetch places the number of rows fetched before the failure occurred in rows_read.

A common reason for a ct_fetch failure is that a program variable specified through ct_bind is too small to hold a fetched data item.

CS_CANCELLED (-202)

The operation was canceled.

ct_fetch places the number of rows fetched before the cancel occurred in rows_read.

CS_ROW_FAIL (-203)

A recoverable error occurred while fetching a row.

Recoverable errors include memory allocation failures and conversion errors that occur while copying row values to program variables.

An application can continue calling ct_fetch to continue retrieving rows, or can call ct_cancel to cancel the remaining results.

ct_fetch places the number of rows fetched before the error occurred in rows_read, then continues by fetching the row after the error.

CS_END_DATA (-204)

No more rows are available in this result set (Note that this is also a successful completion).

TDS_INVALID_PARAMETER (-4)

One of the ct_fetch arguments contains an illegal value.

The most likely cause of this code is assigning a value other than CS_UNUSED to one or more of the reserved arguments, type, offset, and option.

TDS_WRONG_STATE (-6)

Program is in the wrong communication state to issue this call. It is in Send state instead of Receive state.

Examples

Example 1

The following example shows a typical use of ct_fetch. It is taken from the SYCTSAA6 sample program in Appendix A, “Sample Language Application.”

  	/********************************************************************/
  	/*                                                                  */
  	/* Subroutine to fetch row processing                               */
  	/*                                                                  */
  	/********************************************************************/
  	void   fetch_row_processing ()
  	{
   	CS_INT        rows_read;
   	CS_INT        rc;
   	CS_INT        col_len;
   	CS_INT        max_screen_rows = 10;
   	CS_SMALLINT   nomore_rows    = 0;
   	CS_DATAFMT    datafmt;
   	CS_DATAFMT    datafmt2;
   	struct {
           char  first[12];
           char  space2[2];
           char  edlevel[4];
          } output_row;
       while (nomore_rows == FALSE)
       {
         strcpy(col_firstnme.str, "            ");
         memset (&output_row, ' ', sizeof(output_row));
  
         rc = ct_fetch (cmd, (long) CS_UNUSED,
                       (long) CS_UNUSED,
                       (long) CS_UNUSED,
                       &rows_read);
  
         switch (rc)
         {
             case  CS_SUCCEED:
                 nomore_rows         = FALSE ;
                 datafmt.datatype     = CS_VARCHAR_TYPE;
                 datafmt.maxlength    = sizeof(col_firstnme);
                 datafmt2.datatype    = CS_CHAR_TYPE;
                 datafmt2.maxlength   = 12;
 /*------------------------------------------------------------*/
 /* convert the first column from VARCHAR to CHAR for display  */
 /*------------------------------------------------------------*/
                if (cs_convert(context, datafmt, col_firstnme, datafmt2,
                               &output_row.first, &col_len) !=CS_SUCCEED)
                {
                   strncpy (msgstr,
                     "CS_CONVERT CS_CHAR_TYPE failed", msg_size);
                   no_errors_sw = FALSE ;
                   error_out(rc);
                }
  /*------------------------------------------------------------*/
  /* save ROW RESULTS for later display                         */
  /*------------------------------------------------------------*/
  
                 cvtleft  = 4;           /* Digits to the left */
                 cvtright = 0;           /* Digits to the right */
                 SYCVTD(col_edlevel, output_row.edlevel,
                         cvtleft, cvtright, cvtwork, cvtdbl, CS_TRUE);
  
                 if (row_num > max_screen_rows)
                 {
                    strncpy (msgtext1, "Please press return to
                    continue!",text_size);
                    memset (msgtext2, ' ', text_size);
  
                    disp_data ();
  
                    /* re-init output lines */
                    for (row_num = 0; row_num < 14; ++row_num)
                        memset (RS[row_num].rsltno, ' ', text_size);
  
                    row_num = 1;
                    page_cnt = page_cnt + 1 ;
  /*------------------------------------------------------------*/
  /* Setup column headings                                      */
  /*------------------------------------------------------------*/
                    strncpy (RS[row_num].rsltno, "FirstName    EducLvl",
                             text_size);
                    row_num += 1;
                    strncpy (RS[row_num].rsltno, "==========   =======",
                             text_size);
                    row_num += 1;
                 }  /* if row_num > 10 */
  
                 row_num += 1;
                 memcpy (RS[row_num].rsltno, &output_row,
                         sizeof(output_row));
  
                 break; /* end of CS_SUCCEED */
  
             case  CS_END_DATA:
                 nomore_rows = TRUE ;
                 strncpy (msgtext1, "All rows processing completed!",
                          text_size);
                 strncpy (msgtext2, "Press Clear To Exit", text_size);
                 disp_data ();
                 break; /* end of CS_END_DATA  */
  
             case  CS_FAIL:
                 nomore_rows = TRUE ;
                 no_errors_sw = FALSE ;
                 strncpy (msgstr, "CT_FETCH returned CS_FAIL ret_code");
                 error_out(rc);
                 break; /* end of CS_FAIL  */
  
             case  CS_ROW_FAIL:
                 nomore_rows = TRUE ;
                 no_errors_sw = FALSE ;
                 strncpy (msgstr, "CT_FETCH returned CS_ROW_FAIL 
                 ret_code");
                 error_out(rc);
                 break; /* end of CS_ROW_FAIL  */
  
             case  CS_CANCELLED:
                 nomore_rows = TRUE ;
                 no_errors_sw = FALSE ;
                 strncpy (msgstr, "CT_FETCH returned CS_CANCELLED
                 ret_code");
                 error_out(rc);
                 break; /* end of CS_CANCELLED  */
  
             default:
                 nomore_rows = TRUE ;
                 no_errors_sw = FALSE ;
                 strncpy (msgstr, "CT_FETCH returned Unknown ret_code");
                 error_out(rc);
                 break; /* end of OTHERWISE */
  
         }  /* end of switch (rc) */
       }   /* end of while nomore_rows false */
  
  }  /* end fetch_row_processing */
  

Usage


Fetching regular rows


Fetching return parameters


Fetching a return status

See also

Related functions

Related documentation