Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

sql-command on internal table

Former Member
0 Likes
926

Hi,

is it possible to perform sql-command on internal table as below

shown. If not, which alternatives are given.

SELECT COUNT( DISTINCT kunnr ) from itab into table itab2

I have the following situation:

Internal table "itab" contains many datas with various custumers. I want

to collect the unique customernumber in another internal table e.x itab2.

Regards

Ilhan

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
726

Hi Ilhan,

You cannot perform SQL commands on internal tables.

To get the unique customers from an internal table like this:

DATA itab2 LIKE TABLE OF itab.
DATA v_cus TYPE i.

itab2[] = itab[].

SORT itab2 BY kunnr.

DELETE ADJACENT DUPLICATES FROM itab2 COMPARING kunnr.
"itab2 will have unique customers

DESCRIBE TABLE itab2 LINES v_cus.
"v_cus will have the count of unique customers.

Regards

Wenceslaus

5 REPLIES 5
Read only

Former Member
0 Likes
726

hi,

<b>No you cannot use those statements for internal table as SQL Commands are for database tables only ..</b>

Regards,

Santosh

Read only

Former Member
0 Likes
726

No it will not work. SELECT statement will not work on internal tables. It can be used only on database tables and views.

If you want to SUM an internal table field, you will have to loop the table and then sum it up manually.

Manoj

Read only

Former Member
0 Likes
726

it should work....

if not write this way...

SELECT DISTINCT kunnr from <database> into table itab2.

describe table itab2 lines v_lines.....

Read only

Former Member
0 Likes
727

Hi Ilhan,

You cannot perform SQL commands on internal tables.

To get the unique customers from an internal table like this:

DATA itab2 LIKE TABLE OF itab.
DATA v_cus TYPE i.

itab2[] = itab[].

SORT itab2 BY kunnr.

DELETE ADJACENT DUPLICATES FROM itab2 COMPARING kunnr.
"itab2 will have unique customers

DESCRIBE TABLE itab2 LINES v_cus.
"v_cus will have the count of unique customers.

Regards

Wenceslaus

Read only

andreas_mann3
Active Contributor
0 Likes
726

Hi,

no, you must do sth like this:

loop at itab into itab2. 
 collect itab2.
endloop.

describe table itab2 lines sy-tfill.

A.