* ----------------------------------------------------------------------------------- * Dynamic SUM( ) in openSQL * ----------------------------------------------------------------------------------- * You want to sum the values of a table's column, but decide at runtime WHICH of * the columns actually is to be summed up (e.g. by a decision of the user) * In ABAP openSQL, it is possible to have a dynamic expression for the columns * to be selected. This idiom, in principle, can be used to compute a sum dynamically * But there are two things to have in mind: * - the SQL syntax checker doesn't now that the dynamic colexp uses an aggregate function * so it complains that it expects an ENDSELECT, unless you specifiy "select single" * - the string template for the column expression cannot be specified directly in the * select statement (again, the syntax checker is disturbed - this time by the round * brackets inside the literal part of the string template). * Therefore, the auxiliary variable lv_colexp is necessary for this solution. * ----------------------------------------------------------------------------------- data: lv_count type i. data(lv_sumfield) = 'NBRTAB'. data(lv_colexp) = |sum( { lv_sumfield } )|. select single (lv_colexp) from cccflow where uname = 'DDIC' into @lv_count. write / lv_count.