Use strings for DB selections
Today, while refactoring an old report, It came to create a dedicated method for a select to MARC in a dedicated db access class. The selection is done using a select range for material numbers.
Until now, I always proceeded like this:
- search a ready-made ranges type for the needed data type (there is RANGE_T_MATNR)
- if not found, declare a ranges type myself
- use the type for passing the selection range.
Now, depending on standard SAP DEV objects (be it function modules, classes, or DDIC types) always brings the slight danger of code crashes when something changes in the standard system. I also made already bad experience when trying to bring some home-made basic programming tools from ERP to other systems like APO or SCM. Many DDIC objects do not exist there.
So I thought of reducing the dependencies. What about a “range of string”? Will it work in a selection? I wasn’t really sure, so I gave it a try:
First, I defined the interface of my method using generic standard tables for the ranges:
methods read_plant_materials importing plant type string material_range type standard table returning value(result) type string_table.
Then, I call the method with
method test. data material_range type range of string. material_range = value #( ( sign = 'I' option = 'CP' low = `00000000000002*` ) ). data(result) = iut->read_plant_materials( plant = `0001` material_range = material_range ). endmethod.
As you see, also the plant (usually I used the type WERKS_D) for it, is being passed as string. Furthermore I use a string table to capture the results of the select.
The select itself looks pretty normal:
method zif_pp62_db_access~read_plant_materials. select matnr from marc where matnr in @material_range and werks = @plant and lvorm = @space into table @result. endmethod.
The test passes with no problems.
What do you think about this approach? Am I causing maybe much cost for converting everything from and to strings? Let me know!