Read table and loop at
Hi EXpert,
I have an internal table say itab and I need to fetch a single record with some given conditions.
Hence I have used READ table statement ,
But on using this read table statement , I am unable to fetch the record :-(((( . It gives sy-subrc = 4.
And the same conditions when are put in Loop at itab statement, then I am able to fetch the data .
My requirement is only 1 record for the given conditions ( in where clause in case of loop at statement).
I think I am probably missing out on some minor thing..
<removed_by_moderator>
Warm Regards,
SUDHA
Edited by: Julius Bussche on Feb 7, 2009 2:48 PM
Hi,
Test the following Code hope it will solve out your problem,
PARAMETERS: key TYPE i .
TYPES: BEGIN OF t_test,
s_no TYPE i,
name(15),
END OF t_test.
DATA: it_test TYPE STANDARD TABLE OF t_test WITH HEADER LINE.
DO 10 TIMES.
it_test-s_no = sy-index.
it_test-name = 'SDN'.
APPEND it_test TO it_test.
ENDDO.
READ TABLE it_test INTO it_test WITH KEY key.
IF sy-subrc = 0.
WRITE: 'sy-subrc = ' , sy-subrc,
/1 it_test-s_no, 15 it_test-name.
ELSE.
WRITE: 'sy-subrc = ' , sy-subrc,
/ 'Sorry there is no Records with this Key'.
ENDIF.
Kind Regards,
Faisal
Edited by: Faisal Altaf on Feb 7, 2009 6:59 PM
Similar Messages
-
Dear All,
Please let me know which one of the two should I use to improve the performance, for tables containing a lot of data ?
Regards,
Thanks in anticipation.
Alok.Hi,
Follow below steps.
In se30 transaction you can look for
Tip&TRicks button on application toolbar
apart from below conventions
Follow below steps
1) Remove corresponding from select satement
2) Remove * from select
3) Select field in sequence as defined in database
4) Avoid unnecessary selects
i.e check for internal table not initial
5) Use all entries and sort table by key fields
6) Remove selects ferom loop and use binary search
7) Try to use secondary index when you don't have
full key.
8) Modify internal table use transporting option
9) Avoid nested loop . Use read table and loop at itab
from sy-tabix statement.
10) free intrenal table memory wnen table is not
required for further processing.
11)
Follow below logic.
FORM SUB_SELECTION_AUFKTAB.
if not it_plant[] is initial.
it_plant1[] = it_plant[].
sort it_plant1 by werks.
delete adjacent duplicates from it_plant1 comparing werks
SELECT AUFNR KTEXT USER4 OBJNR INTO CORRESPONDING FIELDS OF TABLE I_AUFKTAB
FROM AUFK
FOR ALL ENTRIES IN it_plant1
WHERE AUFNR IN S_AUFNR AND
KTEXT IN S_KTEXT AND
WERKS IN S_WERKS AND
AUART IN S_AUART AND
USER4 IN S_USER4 AND
werks eq it_plant1-werks.
free it_plant1.
Endif.
ENDFORM. "SUB_SELECTION_AUFKTAB
Regards
Amole -
How to read tables and fields transaction,how to find table from a strucre
hi all,
i am having problem in reading tables and fields for developing a customised report. can anybady help me how to extract tabele and fields from a transaction code and how to map table from a structure.
It will me much help full, if u had any documentation. u should be appreciated.
Thanking u
kiran
Message was edited by:
kiranHi Kiran,
You can make use of the tables or Views available.
Reward If Useful.
Regards,
Chitra -
"Read table" and field symbols
Hello all,
I have a (hopefully simple) question. Let's suppose I want to do the following:
loop at itab1 assigning <fs1>.
read table itab2 assigning <fs2> with table key <fs1>-field1.
if sy-subrc eq 0.
move <fs2>-field1 to ls_out-field1.
endif.
read table itab3 assigning <fs3> with table key <fs1>-field2.
if sy-subrc eq 0.
move <fs3>-field1 to ls_out-field2.
endif.
endloop.
It's also possible to do the following instead:
loop at itab1 assigning <fs1>.
unassign: <fs2>,
<fs3>.
read table itab2 assigning <fs2> with table key <fs1>-field1.
read table itab3 assigning <fs3> with table key <fs1>-field2.
if <fs2> is assigned.
move <fs2>-field1 to ls_out-field1.
endif.
if <fs3> is assigned.
move <fs3>-field1 to ls_out-field2.
endif.
endloop.
My question is: is it "better" to check sy-subrc after each read, and then do a move statement within that if statement, or is it better to check whether the field symbol is assigned each time I want to fill in a field and read everything at the same time?
I'm only really concerned about the case where you don't want to drop out of the loop after the read (so if the read fails you just don't fill any data in). I'm also discounting potential human error (otherwise the sy-subrc method wins hands-down).Just to close this one (albeit somewhat belatedly). I asked Harry Dietz (the ABAP Performance blogging fella) what he would suggest, and he says sy-subrc all the way. Checking that an integer is zero is considerably faster than checking for the assignment, or otherwise, of a field symbol. Furthermore, whilst the field symbol method has the potential to be faster than the sy-subrc method given some compiler optimizations, unfortunately these optimizations don't exist in the ABAP compiler - they're there in the Java compiler though! Shame really. Apparently the ABAP compiler is a pretty simple beast.
Anyway - thanks Harry for clearing this one up! -
Draw the Borders on the Table and Loop Contents.
Hello Everyone,
I have a Table and a Loop in the Smartform. I am Printing the Details on the Tabular Format of 5 coulums and a few rows in the table and a loop.
Now, I want to Draw the table lines for these tables and the Loop.
The Option of Boxes and Lines are provided but I can not use them.
Can any one please Suggest me how to use the Lines and the Boxes on the table and the Loop.
Regards,
Deepak.Hello,
I tried the way you told me to but then every time I single click or even double click. It just gives me the same error that
"The Boxes and shadowing are not allowed in the Table."
I am doing this in the Output options tab of the Cell which has been created under the Row line of the table.
Please suggest me any other possible work around or the solution. Many thanks for your Advice so far.
Regards,
Deepak. -
Hi all,
I have a simple query regarding read int_tab with binary search.
Why reading internal table with binary search fails if it is sorted in descending order table must be sorted in ascending order?
I check fo the algorithm of binary search, it does not talk about sort order. As far as my understanding goes binary search only require sorted table but while reading table in SAP it has to be sorted in ascending order!!By default binary search assumes that the sort order is ASCENDING.
If you sort the list in descending and then try to binary search your quires will fail. Look at an example:
Let the descending order internal table as:
Field1 Field2
Sam 50000
John 34786
Boob 54321
Alice 12345
When you do binary search with key = 'Sam' then it will directly go to 2nd and 3rd records for comparision. The binary search algorithm compares 'Sam' with 'John' and it concludes that 'Sam' is greater than 'John' and it will continue to look downward into the internal table. And when it reaches the end of the internal table then the value of SY-TABIX = 5 and SY-SUBRC = 8 (Key is greater than the all).
And if you do binary search with key = 'Alice' then the binary search algorithm compares 'Alice' with 'John' and it concludes that 'Alice' is lower than 'John' and it will continue to look upward into the internal table. And when it reaches above the first record in internal table then the value of SY-TABIX = 1 and SY-SUBRC = 4 (points to the next largest entry).
The only correct result you will get is when you execute statement with key='John' (In this particular case) . SY-TABIX = 2 and SY-SUBRC = 0. I think you got this binary search algorithm. -
How to read tables and fields from a transaction screen.
hi all,
i am having problem in reading fields and tables from a screen, could any one help me in mapping tables from a structure as well.
it will be very help full if u had any documentation.
Thanking u
kiranHi,
In classical debug mode from CALL STACK tab try to find out the structures holding the screen values .
Capture the values from transaction using FIELD SYMBOLS .
See the below code.
DATA : l_prog_val(50) TYPE c VALUE '(SAPLCTMS)WI[]'.
**To get the (SAPLCTMS)WI value in this program from call stack.
FIELD-SYMBOLS: <wi> TYPE ANY.
*Assign the structure (SAPLCTMS)WI to field symbol
ASSIGN (l_prog_val) TO <wi>.
IF sy-subrc NE 0.
EXIT.
*Assign the structure (SAPLCTMS)WI to internal table
ELSE.
it_wi[] = <wi>.
ENDIF.
Sastry. -
Read table and checking condition in PAI
Hi,
I have a requirement where I need to check and compare with (Custom table) Bankn with Partner Bank`s bankn.
I have wrote a code in PAI like this.
MODULE CHECK_BANKN INPUT.
Data: l_bankn like (Custom table)-bankn.
select bankn from (Custom Table) into l_bankn
where recno = g_recno.
IF NOT g_head-bankn IS INITIAL.
Read table i_bvtyp with key bankn = i_bvtyp-bankn .
IF l_bankn <> i_bvtyp-bankn.--------->" Comparison of (custom table) Bankn with internal table I_BVTYP-BANKN
MESSAGE w022 WITH text-w06.
ENDIF.
ENDIF.
ENDMODULE.
My Read Table statement is seems to be wrong and my checking condition is also wrong.
So, how to resolve this?
regards,
KiranHi Kiran,
If you observe your read statement, you are comparing the field with it's own table field.
Read table i_bvtyp with key bankn = i_bvtyp-bankn .
Untill unless you read I_BVTYP, i_bvtyp-bankn will be initial and you cannot find the matching record with Null-Value.
So, I think you need compare with l_bankn.
Read table i_bvtyp with key bankn = i_bankn .
Then you can directly raise a message using SY-SUBRC value.
Read table i_bvtyp with key bankn = i_bankn
if SY-SUBRC <> 0 .
<MESSAGE>
endif.
Regards,
Vijay -
Update records from a table in correct sequence that look from 2 tables and loop
Hi!
My question title is kinda unclear but here how it goes.
I created 2 tables for my BOM (Bill of Materials). I have BOM_Header and BOM_Detail.
Below are my sample data.
BOM_Header
Product_ID Int
Product_Name Varchar(50)
Cost Numeric(18,2)
Yield Varchar(50)
Select * from BOM_Header
1 Choco Cake 850.00 10
2 Mixed Flour 700.00 30
3 Choco Syrup 160.00 10
4 Egg Formula 2150.00 20
BOM_Detail
Product_ID int
ItemNo Int
ItemName varchar(50)
Quantity int
Unit varchar(50)
ProdCost numeric(18,2)
Select * from BOM_Detail
1 2 Mixed Four 10 Grams 15.00
1 3 Choco Syrup 20 ML 25.00
1 4 Egg Formula 20 Grams 10.00
2 101 Flour 5 packs 80.00
2 4 Egg Formula 5 Grams 60.00
3 201 Cocoa Power 2 kg 20.00
3 202 Sugar 2 kg 60.00
4 301 Whole Egg 10 pcs 85.00
4 302 EP12 Formula 25 ml 52.00
My computation is below.
BOM_Header = a
BOM_Detail = b
a.Cost = b.Quantity x b.Product Cost where a.Product_ID = b.Product_ID
My problem is how can I automatically compute their Food Cost in sequence from raw materials to finished products.
Based on the data, I need to compute first the Egg Formula because it is used as component of Mixed Flour then compute the Mixed Flour and other component to get the cost of Choco Cake.
How can I do this automatically in query to look first in detail if there are ingredients within a sub - ingredients then compute before computing the final cost of the Product.
This is because cost of ingredients are changing most of the time and need to recalculate to get the most updated cost.
Any suggestion is very much appreciated.
Thank you very much,
Regem>> My question title is kinda unclear but here how it goes. <<
Then your answers will be unclear, too :(
You do not know data modeling, so your data types are wrong. What math are you doing with the product_id? None. This is why identifiers are char(n) and not numeric. They should be industry standards if possible. I like the GTIN.
You do not even know that rows are not records.
Why is the product name fifty characters? Certainly not research! But if you are an ACCESS programmer using a default vale, then you might do this.
Besides violating ISO-11179 rules, “<vague>_field” makes no sense! It is a quantity that you put in a string.
CREATE TABLE BOM_Header
(gtin CHAR(15) NOT NULL PRIMARY KEY,
product_name VARCHAR(25) NOT NULL
unit_cost DECIMAL(18,2) NOT NULL
CHECK ( unit_cost >= 0.0.),
yield_qty INTEGER NOT NULL
CHECK (yield_qty >= 0));
>> Any suggestion is very much appreciated. <<
Get a copy of my book on TREES in SQL and read the chapter on BOM problems. I am not goingto try to post a whole chaper and diagrams to answer this. You are doing the wrong things and have done them poorly.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Can you read Reports and Tables through RFCs and BAPIS?
Is there any way to read established reports and tables through RFC or BAPI function calls? We have a middleware that can execute various BAPIs and RFCs. I see a few promising ones: RFC_READ_REPORT, but no examples anywhere what the parameters needed are for the inputs.
Hello Scott,
Check below FMs for reading tables and programs.
Tables : RFC_READ_TABLE
Program : RFC_READ_DEVELOPMENT_OBJECT
Please note that for reading program using RFC_READ_DEVELOPMENT_OBJECT , lines in program should not exceed 72 chars else FM will throw dump(READ_REPORT_LINE_TOO_LONG).
Program code will appear in table QTAB which is 72 chars wide. -
Retrieve data from two tables and store it in one itab
Hi All,
i have an internal table of structure which has fields ar_object and objecttext.
I want to retrieve all the ar_object from table toaom for object type BUS2012 and then the corresponding objecttext from toasp where toaom-ar_object = toasp-ar_object and then store both this values in my internal table.
what is the best method to do it ? Please let me know if you want to know how i am doing it right now
thanks,
GoluHi,
You can get many ways
1. using For all entries in
2. using Inner join
3. you can get 2 tables data into 2 different internal tables and loop first internal table and read second internal using key fields append it another internal table or modify second internaltable.
In above ways 1st and 2nd are performance issues. the best way is
get TOAOM table data into itab1 and get TOASP table data into itab2 you can append data into another itab3 or modify itab2
regards,
Ganesh -
Can any body explain about Read table and when it can use.
Hi,
When u want to read a record based on some condition then u can use READ.
LOOP AT itab.
READ TABLE itab WITH KEY matnr = itab-matnr.
IF sy-subrc = 0.
ENDIF.
ENDLOOP.
U can read only a single record.
U can also use
READ TABLE itab INDEX 1.
Reward points if this is helpful. -
Sy-tabix in relation to LOOP AT and READ TABLE
Hi All,
As per SAP documentation,
1) While looping through an internal table (LOOP AT), sy-tabix contains the index number of current row(for standard and sorted tables)
2)When successfully reading from an internal table(READ TABLE), sy-tabix is set to the index of the result row.
But what happens when READ TABLE is used while looping through another internal table?
i.e. Loop at TAB1...
write sy-tabix.
READ TABLE TAB2...
write sy-tabix.
endloop.
If we are looping through 1st row of TAB1 and the result of read statement is found in 3rd row of TAB2, I expected that sy-tabix before READ would be 1 and after the READ be 3.
But, I found that sy-tabix remains unchanged at 1. Can someone expalin why?
Thanks,
JaganHi
If after reading the table TAB2 the system variable SY-TABIX has still the previous value, that menas the READ TABLE fails or it was read the first record of TAB2.
After READ TABLE TAB2 try to check the SY-SUBRC:
LOOP AT TAB1.
WRITE: / 'TAB1 index:', SY-TABIX.
READ TABLE TAB2 .........
IF SY-SUBRC = 0.
WRITE: 'TAB2 index:', SY-TABIX.
Try this:
DATA: BEGIN OF ITAB OCCURS 0,
FIELD1,
END OF ITAB.
DATA: BEGIN OF ITAB2 OCCURS 0,
FIELD1,
END OF ITAB2.
DATA: INDEX TYPE I.
DO 10 TIMES.
APPEND ITAB.
ENDDO.
DO 10 TIMES.
APPEND ITAB2.
ENDDO.
LOOP AT ITAB.
WRITE: / 'ITAB:', SY-TABIX.
INDEX = SY-TABIX + 2.
READ TABLE ITAB2 INDEX INDEX.
IF SY-SUBRC = 0.
WRITE: 'ITAB2:', SY-TABIX.
ENDIF.
ENDLOOP.
Max -
Performance syntax loop at and read table
in the routine , for reading one line in a internal table , the syntaxe
loop at xxx where and read tabl exxx with key XXXX
has a great difference on performance or not?Loop at statement is used only for processing multiple records.Read table is used for reading a particluar record of an internal table.If you just need to check whether record exists in internal table, use can sort and use binary search with TRANSPORTING NO FIELDS addition. Also, try to use field symbols so that performance is increased.
-
I need to loop through 1 table and read another until a value changes
i need to read a table and sum the quantity field until a reason code changes. how do I go about doing this?
sort itab by reasoncode.
Loop at itab.
quantiy = quanity + itab-quantity.
at end of reasoncode.
jtab-reasoncode = itab-reasoncodee.
jtab-sum = quantity.
append jtab.
clear quantity.
endat
endloop.
or
sort itab by reasoncode.
loop at itab.
at end of reasoncode.
sum.
jtab = itab.
append jtab.
endat.
endloop.
or
let us say itab and jtab are two tables and you want to loop through itab and read jtab for reasoncodes.
if jtab has only one entry for each entry in itab then use read else use loop.
loop at itab.
loop at jtab where reasoncode = itab-reasoncode.
quantiy = quantiy + jtab-quanity.
endloop.
endloop.
or
loop at itab.
read table jtab with key reasoncode = itab-reasoncode.
if sy-subrc eq 0.
endif
endloop.
Maybe you are looking for
-
Serial # does not work. Chat is always unavailable.
Hello, 1. I bought Adobe Creative Suite 5 Design Standard in 2010. 2. I have not tried to initiate Adobe Premiere until now. 3. The serial number I had originally does not work. 4. I have already contacted help (from the registration screen) and rece
-
Why is my 2011 macbook freezing after unplugging from tv?
Hi Everybody! Ive searched the forums for this but couldnt find anything the same. However, this is my first post so forgive me if i missed an answer to this already. Lately, i've been plugging my early 2011 13" Macbook Pro (i7 running lion) into my
-
i cant back up my ip5 because i forgeted password. how i can take my password. help me. tks
-
Forcing a JTextPane to scroll down
Hello I have a JTextPane inside a JScrollpane how can I force the JTextPane to scroll down when text is added to it?
-
Heay guys, every time i start After Effects i get a error .... I tried everything .... Maybe you can help me ? PC Specs: intel xeon e3-1231v3 GTX 970 16gb Ram If you need informations tell me