utorok 31. januára 2012

Select data from database using Wildcard characters

The following example shows how to enhance SELECT statements with the use of Wildcard characters.

Wildcard characters used:
  • "%" represents any character string, even an empty one.
  • "_" represents any character
In the example shown below, we try to read data from table MINE_TABLE where the 2nd and 3rd character = "AB".
 
* Structure & Data of MINE_TABLE:
-------------------------
|    ID    |    TEXT    |
-------------------------
| 1AB001   | line #1AB  |
-------------------------
| 1AB002   | line #2AB  |
-------------------------
| 1AC001   | line #1AC  |
-------------------------
| 1AD001   | line #1AD  | 
-------------------------

* Local variables
DATA:
    lt_table TYPE STANDARD TABLE OF mine_table,
    ls_table LIKE LINE OF lt_table.
DATA:
    lv_id    TYPE string.

* Define the use of wildcard characters
CLEAR: lv_id.
CONCATENATE '_' 'AB' '%' INTO lv_id.

* Select data from dB MINE_TABLE with a Wild-Card
REFRESH: lt_table.
SELECT *
  INTO CORRESPONDING FIELDS OF lt_table
  FROM mine_table
  WHERE id LIKE lv_id.


* Write the content of internal table LT_TABLE
CLEAR: ls_table.
LOOP AT lt_table IN ls_table.
  WRITE:/ ls_table-id, ' - ', ls_table-text.
ENDLOOP.

This is the output on the screen:
1AB001 - line #1AB
1AB002 - line #2AB

pondelok 30. januára 2012

Processing of Dynamically created table (with TYPE REF TO DATA).

The current post represents a method of how:
  1. to get reference of dynamically created internal table. The example suggest an internal table of type range, which structure consists of fields 'SIGN', 'OPTION', 'LOW', 'HIGH'.
  2. to get reference of dynamically created work area - based on a type of dynamically created internal table.
  3. to set fields of dynamically created work area with default values.
  4. to insert dynamically created work area to dynamically created internal table.
* Local variables:
  DATA:
      lref_table TYPE REF TO data,
      lref_wa    TYPE REF TO data.
  FIELD-SYMBOLS:
      <fs_table> TYPE ANY TABLE,
      <fs_wa>    TYPE ANY,
      <fs_field> TYPE ANY.

* Dynamically created table with the use of an class from WebDynpro ABAP:
* 'MINE RANGE' is defined as data element in Data Dictionary.
  lref_table = 
    lref_helper_class->create_range_table( i_typename = 'MINE_RANGE' ).
  FIELD-SYMBOLS <fs_table> type ANY TABLE.
  FIELD-SYMBOLS <fs_wa> TYPE any.
  data lref_wa TYPE REF TO data.


* Get reference of dynamic internal table
  ASSIGN lref_table->* TO <fs_table>.
* Create dynamic work-area for dynamic table
  CREATE DATA lref_wa LIKE LINE OF <fs_table>.

* Get reference of dynamic work area
  ASSIGN lref_wa->* TO <fs_wa>.





* Since we have dynamic RANGE table, work-are structure consists of components
* as 'SIGN' & 'OPTION' & 'LOW' & 'HIGH'.
  ASSIGN COMPONENT 'SIGN'   OF STRUCTURE <fs_wa> TO <fs_field>.
  <fs_field> = 'I'.
  ASSIGN COMPONENT 'OPTION' OF STRUCTURE <fs_wa> TO <fs_field>.
  <fs_field> = 'EQ'.
  ASSIGN COMPONENT 'LOW'    OF STRUCTURE <fs_wa> TO <fs_field>.
  <fs_field> = 'xxx'.



* Insert dynamic work-area into dynamic table
  INSERT <fs_wa> INTO TABLE <fs_table>.

A cl_alv_table_create with a method create_dynamic_table may be used to create internal table dynamically with predefined field-catalog.