Advertisement
rplantiko

Dynamic SUM( ) in open SQL

Sep 20th, 2018
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 1.33 KB | None | 0 0
  1. * -----------------------------------------------------------------------------------
  2. * Dynamic SUM( ) in openSQL
  3. * -----------------------------------------------------------------------------------
  4. * You want to sum the values of a table's column, but decide at runtime WHICH of
  5. * the columns actually is to be summed up (e.g. by a decision of the user)
  6. * In ABAP openSQL, it is possible to have a dynamic expression for the columns
  7. * to be selected. This idiom, in principle, can be used to compute a sum dynamically
  8. * But there are two things to have in mind:
  9. * - the SQL syntax checker doesn't now that the dynamic colexp uses an aggregate function
  10. *   so it complains that it expects an ENDSELECT, unless you specifiy "select single"
  11. * - the string template for the column expression cannot be specified directly in the
  12. *   select statement (again, the syntax checker is disturbed - this time by the round
  13. *   brackets inside the literal part of the string template).
  14. *   Therefore, the auxiliary variable lv_colexp is necessary for this solution.
  15. * -----------------------------------------------------------------------------------
  16.  
  17.   data: lv_count type i.
  18.   data(lv_sumfield) = 'NBRTAB'.
  19.  
  20.   data(lv_colexp) = |sum( { lv_sumfield } )|.
  21.   select single (lv_colexp) from cccflow where uname = 'DDIC' into @lv_count.
  22.   write / lv_count.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement