Fetch a single field from DB table into itab
Asked Answered
A

3

5

I want to fetch the a field say excep_point from a transparent table z_accounts for the combination of company_code and account_number. How can I do this in ABAP SQL?

Assume that table structure is

|company_code | account_number | excep_point |
Arango answered 1/10, 2010 at 14:13 Comment(0)
S
5

Assuming you have the full primary key...

data: gv_excep_point type zaccounts-excep_point.

select single excep_point
into gv_excep_point
from zaccounts 
where company_code = some_company_code
 and account_number = some_account_number.

if you don't have the full PK and there could be multiple values for excep_point

data: gt_excep_points type table of zaccounts-excep_point.

select excep_point
into table gt_excep_points
from zaccounts 
where company_code = some_company_code
 and account_number = some_account_number.

There is at least another variation, but those are 2 I use most often.

Stevenstevena answered 1/10, 2010 at 17:15 Comment(0)
E
3

For information only. When you selects data into table you can write complex expressions to combine different fields. For example, you have internal table (itab) with two fields "A" and "B". And you are going to select data from DB table (dbtab) wich have 6 columns - "z","x","y","u","v","w". And for example each field is type char2 You aim to cimbine "z","x","y","u" in "A" field of internal table and "v","w" in "B" field. You can write simple code:

select z as A+0(2)   
       x as A+2(2)   
       y as A+4(2)  
       u as A+6(2)   
       v as B+0(2)  
       w as B+2(2)  FROM dbtab  
       INTO CORRESPONDING FIELDS OF TABLE itab
          WHERE <where condition>.

This simple code makes you job done very simple

Elijaheliminate answered 2/2, 2011 at 13:33 Comment(1)
select z as A+0(2) x as A+2(2) ... is very nice, never have seen this tricky selection method. Thank you alot.Dardanus
T
1

In addition to Bryans answer, here is the official online documentation about Open SQL.

Tsarevna answered 1/10, 2010 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.