Sorgu ile rastgele veri çekme
Doğrudan SQL ile rastgele 100 satır çekebilmek için
- dd03L tablosundan tablonun alanları dahili tabloya çekilir
- İndeks için dahili tablonun boyutunu aşmayacak rastgele bir sayı üretilir
- Alan adları tablosundan üretilen indeksteki satır okunur.
- Üretilen rastgele sütun adı, sorguya dinamik olarak verilir
types: begin of ty_customer,
kunnr type kunnr,
name1 type name1_gp,
end of ty_customer.
TYPES: tTY_customer type SORTED TABLE OF ty_customer with unique key kunnr.
data: gt_customer type tTY_customer.
PARAMETERS: p_ccust type int4 DEFAULT 10 OBLIGATORY.
form f_get_random_field USING prm_table TYPE TABNAME CHANGING chn_field TYPE FIELDNAME.
data: lv_seed type i ,
lv_random type i,
lc_rnd_class type ref to cl_abap_random_int.
data: lt_dd03L type STANDARD TABLE OF dd03L .
data: ls_dd03L type dd03L .
data: lv_maxsize type int4.
CLEAR chn_field.
select * into TABLE lt_dd03L from dd03L where tabname = prm_table and not ( fieldname = `.INCLU--AP` or fieldname = `.INCLUDE` or fieldname LIKE `/*` ).
CHECK lt_dd03L[] is not INITIAL.
DESCRIBE TABLE lt_dd03L LINES lv_maxsize.
lv_seed = cl_abap_random=>seed( ).
lc_rnd_class = cl_abap_random_int=>create( seed = lv_seed min = 1 max = lv_maxsize ).
lv_random = lc_rnd_class->get_next( ).
READ TABLE lt_dd03L into ls_dd03L index lv_random.
CHECK sy-subrc eq 0.
chn_field = ls_dd03L-fieldname.
endform.
form f_fill_customer .
data: ls_customer type ty_customer.
data: lv_size type ty_customer.
data: lv_sort_field TYPE FIELDNAME.
refresh gt_customer.
perform f_get_random_field USING `KNA1` CHANGING lv_sort_field.
select KUNNR name1 into TABLE gt_customer from kna1 up to p_ccust rows
ORDER BY (lv_sort_field) .
"Random tablo : gt_customer
endform. "read_data_FUNCSPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; }.L0S32 { color: #3399FF; }.L0S33 { color: #4DA619; }.L0S52 { color: #0000FF; }.L0S55 { color: #800080; }.L0S70 { color: #808080; }