Processing compute results

A compute result set is generated by the execution of a Transact-SQL select statement that contains a compute clause. A compute clause generates a compute result set every time the value of its bylist changes. A compute result set consists of a single row containing a number of columns equal to the number of row aggregates in the compute clause.

For example, consider the query:

select type, price from titles 
 where price > $12 and type like "%cook" 
 order by type, price compute sum(price) by type

The query returns regular rows (with columns type and price). Intermixed with the regular rows, the query returns compute result sets each time the value of type changes in the regular row results. Each compute result set contains a single row with one column for the sum(price) expression.

See the Adaptive Server Enterprise Reference Manual for more examples of queries with a compute clause.

In addition to ct_res_info, ct_describe, ct_bind, and ct_fetch, an application can call ct_compute_info while processing compute row results. ct_compute_info provides a variety of compute row information. The information available from ct_compute_info includes:

Most synchronous applications use a program structure similar to the following one to process a compute result set:

 case CS_COMPUTE_RESULT
     (optional)ct_compute_info to get bylist length,           
     bylist, or compute row id
     ct_res_info(CS_NUMDATA) to get the number of columns
     for each column:
         ct_describe to get a description of the column
         ct_bind to bind the column to a program variable
         (optional: ct_compute_info to get the compute 
             column id or the aggregate operator for the
             compute column)
     end for
     while ct_fetch returns CS_SUCCEED or CS_ROW_FAIL
         if CS_SUCCEED
             process the compute row
         else if CS_ROW_FAIL
             handle the failure
         end if
     end while
     switch on ct_fetch’s final return code
         case CS_END_DATA...
         case CS_CANCELED...
         case CS_FAIL...
     end switch
 end case