Nested looping with select stmt
Hi Gurus,
I need to create an abap program that takes i/p a database table(dbtable contains list of table names).The expected o/p is a DB table with tablename,co_code,total amt in local curr,amount in doc curr.
the following is my logic:
Select * from zmatlist into table it_tablist where zmatlist~tflag like ''. "condition to get list of table names from DB table
Loop at it_tablist into wa_tablist.
Select [distinct]co_code from (wa_tablist-tabname) into table it_cocode.
Loop at it_cocode into wa_cocode.
select amount curtype from (wa_tablist-tabname) into table it_amount where co_code = wa_cocode-cocode.
loop at it_amount into wa_amount.
if curtype eq '00'.
sum = sum + wa_amount-amount.
else.
total = total + wa_amount-amount.
endloop.
wa_result-tabname = wa_tablist-tabname.
wa_result-cocode = wa_cocode-cocode.
wa_result-AMTLC = sum.
wa_result-AMTDC = total.
Append wa_result to it_result.
Clear: sum,
total.
refresh: it_amount.
endloop.
refresh it_cocode.
Endloop.
when i am executing it,it is not able to recognise amount field.How to refer amount ,cocode here?
Am i writing it so complex?Is there any easy way to do it.Plz let me know if there is any.
Thanks,
Vijay.
Hi Vijay,
Welcome to SDN
Have you tried using field symbols for your requirement.
regards,
Atish
Similar Messages
-
Error with select stmt.
Hi All,
I am getting this error , can you please tell me what I am doing wrong with the select stmt.
SELECT AFKDAT ANETWR B~KZWI1
FROM VBRK AS A INNER JOIN VBRP AS B ON
AVBELN = BVBELN
INTO (IOUT-FKDAT, IOUT-NETWR, IOUT-KZWI1)
WHERE B~VBELN = IOUT-VBELV
AND B~POSNR = ISDOC-POSNR.
Incorrect nesting: Before the statement "ENDIF", the structure
introduced by "SELECT" must be concluded by "ENDSELECT". -
Thanks
Veni.Hi All,
I tried INTO CORRESPONDING FIELDS OF IOUT
but with this also same error is comming. I am sending part of code.
Please help me.
Thanks
Veni.
TABLES: VBAK, VBAP, KNA1, VBRK, VBRP, VBFA.
DATA: BEGIN OF ISDOC OCCURS 0,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBAP-POSNR,
KUNNR LIKE VBAK-KUNNR,
ERDAT LIKE VBAK-ERDAT,
BSTNK LIKE VBAK-BSTNK,
MATNR LIKE VBAP-MATNR,
ARKTX LIKE VBAP-ARKTX.
DATA: END OF ISDOC.
DATA: BEGIN OF IOUT OCCURS 0,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBAP-POSNR,
KUNNR LIKE VBAK-KUNNR,
NAME1 LIKE KNA1-NAME1,
ERDAT LIKE VBAK-ERDAT,
BSTNK LIKE VBAK-BSTNK,
MATNR LIKE VBAP-MATNR,
ARKTX LIKE VBAP-ARKTX,
VBELV LIKE VBRP-VBELN,
FKDAT LIKE VBRK-FKDAT,
NETWR LIKE VBRK-NETWR,
KZWI1 LIKE VBRP-KZWI1.
DATA: END OF IOUT.
FORM getdata.
SELECT A~VBELN B~POSNR A~KUNNR A~ERDAT A~BSTNK B~MATNR B~ARKTX
FROM VBAK AS A INNER JOIN VBAP AS B ON A~VBELN = B~VBELN
INTO TABLE ISDOC
WHERE A~AUART IN SAUART
AND A~ERDAT IN SERDAT
AND A~KUNNR IN SKUNNR.
SORT ISDOC BY VBELN POSNR.
LOOP AT ISDOC.
MOVE-CORRESPONDING ISDOC TO IOUT.
CLEAR: KNA1.
SELECT SINGLE * FROM KNA1 WHERE KUNNR = ISDOC-KUNNR.
IF SY-SUBRC = 0.
MOVE KNA1-NAME1 TO IOUT-NAME1.
ENDIF.
SELECT VBELN INTO IOUT-VBELV FROM VBFA WHERE VBELV = ISDOC-VBELN
AND POSNV = ISDOC-POSNR
AND VBTYP_N = 'O'.
IF SY-SUBRC = 0.
SELECT A~FKDAT A~NETWR B~KZWI1
FROM VBRK AS A INNER JOIN VBRP AS B ON A~VBELN = B~VBELN
INTO CORRESPONDING FIELDS OF IOUT
* (IOUT-FKDAT, IOUT-NETWR, IOUT-KZWI1)
WHERE B~VBELN = IOUT-VBELV
AND B~POSNR = ISDOC-POSNR.
APPEND IOUT.
CLEAR IOUT.
ENDIF.
ENDLOOP.
ENDFORM. " getdata -
Hi All,
Do you see anything wrong with this select stmt, it is giving me short dump.
DATA: BEGIN OF it_output OCCURS 0,
check TYPE c,
vbeln LIKE likp-vbeln,
lfdat LIKE likp-lfdat,
kunnr LIKE likp-kunnr,
knkli LIKE likp-knkli,
netwr LIKE vbak-netwr,
END OF it_output.
SELECT LIKPVBELN LIKPLFDAT LIKPKUNNR LIKPKNKLI VBAK~NETWR
FROM LIKP
INNER JOIN VBAK ON LIKPVBELN = VBAKVBELN
INTO TABLE IT_OUTPUT
WHERE LIKP~LFDAT IN S_LFDAT
AND LIKP~KUNNR IN S_KUNNR
AND LIKP~LIFSK = P_LIFSK.
Thanks
Veni.Hi
DATA: BEGIN OF it_output OCCURS 0,
One error is here:
<b>*check TYPE c,</b>
vbeln LIKE likp-vbeln,
lfdat LIKE likp-lfdat,
kunnr LIKE likp-kunnr,
knkli LIKE likp-knkli,
netwr LIKE vbak-netwr,
<b>check type c,</b>
END OF it_output.
SELECT LIKPVBELN LIKPLFDAT LIKPKUNNR LIKPKNKLI VBAK~NETWR
FROM LIKP
Another error is here
<b>*INNER JOIN VBAK ON LIKPVBELN = VBAKVBELN</b>
INNER JOIN LIPS ON LIKPVBELN = LIPSVBELN
INNER JOIN VBAK ON LIPSVGBEL = VBAKVBELN
INTO TABLE IT_OUTPUT
WHERE LIKP~LFDAT IN S_LFDAT
AND LIKP~KUNNR IN S_KUNNR
AND LIKP~LIFSK = P_LIFSK.
The link between sales order and delivery is in the item data, field is LIPS-VGBEL
Max -
Problem with Nested loop in Fox-Formula
Dear Experts,
Let s share the scenario :
MaterialGroups with following Keys for Example AAAA, BBBB, CCCC..., Materialgroups are selected in the 1st input ready query, which is assigned to DataProvider DP_1 in a webtemplate.
every Materialgroup has several Materials, for instance:
Materialgroup AAAA has following Materials: AAAA10, AAAA11, AAAA12, AAAA13...
Materials are selected in a second input ready Query, which is assigned to a second DataProvider DP_2 in the Same Webtemplate as the query 1.
Both Materialgroup and Material are based on the same Aggreagtion level and same real time cube.
I want to copy the input values for every MaterialGroup ( 1st query, DP_1) only to it s own Materials (2cond Query, DP_2).
To resolve this Issue i wrote the following Fox Formula code with a nested loop, however it does not work properly. when I m debugging the code, i could release that the second Loop was ignored.but wehn i replace the second loop (nested loop) with a fixed value it s seems to work properly
DATA MG1 TYPE MATG.<------ MaterialGroup
DATA MT1 TYPE MAT.<----
Material
DATA S1 TYPE STRING.
DATA S2 TYPE STRING.
DATA S3 TYPE STRING
DATA K TYPE F.
DATA Z TYPE F.
FOREACH MG1.
To check Materialgroup in debugger
S1= MG1.
BREAK-POINT.
K = {KEYfIG, #, MG1}.
BREAK-POINT.
FOREACH MT1. <----- if i set MT1 to a fixed value like AAAA11 then S3 get the wished value namely AAAA
S2 = MT1.
BREAK-POINT.
S3 = SUBSTR (MT1, 0, 4).
BREAK-POINT.
IF S1 = S3.
{KEYFIG, MT1, #} = K.
following Statement is only used To check in debugger if Material has become the same Materialgroup value
Z = {KEYFIG, MT1, #}.
BREAK-POINT.
ENDIF.
ENDFOR.
ENDFOR.
Thakns for any help
Frank
Edited by: FRYYYBM on Mar 17, 2011 10:54 PM
Edited by: FRYYYBM on Mar 17, 2011 11:06 PMHi,
Please try this way.
DATA MG1 TYPE MATG.<------ MaterialGroup
DATA MT1 TYPE MAT.<----
Material
DATA S1 TYPE STRING.
DATA S2 TYPE STRING.
DATA S3 TYPE STRING
DATA K TYPE F.
DATA Z TYPE F.
FOREACH MT1.
FOREACH MG1.
To check Materialgroup in debugger
S1= MG1.
BREAK-POINT.
K = {KEYfIG, #, MG1}.
BREAK-POINT.
FOREACH MT1. <----- if i set MT1 to a fixed value like AAAA11 then S3 get the wished value namely AAAA
S2 = MT1.
BREAK-POINT.
S3 = SUBSTR (MT1, 0, 4).
BREAK-POINT.
IF S1 = S3.
{KEYFIG, MT1, #} = K.
following Statement is only used To check in debugger if Material has become the same Materialgroup value
Z = {KEYFIG, MT1, #}.
BREAK-POINT.
ENDIF.
ENDFOR.
ENDFOR.
ENDFOR.
Thanks.
With regards,
Anand Kumar -
Problem in Select stmt in loop
Hi all,
Pls provide your suggestions for the following code.
LOOP at ITAB.
select * from zxyz into corresponding fields of table ITAB_FINAL
where fromdate eq date1
todate eq date2
MATNR eq ITAB-matnr.
ENDLOOP.
My question is that for a particular entry in itab , select stmt is executed and some 5 to 6 line item is selected into itab_final. Now in second loop , again select stmt is executed and again some line item will be selected , BUT will this selection will replace the earlier entry in ITAB_FINAL OR the entry in SUCCESSIVE loop will be APPENDED ?
Please suggest your valuable suggestion on this problem
Thanks in advance,
Regards,
VivekHi,
There is no need to loop if you are using FOR ALL ENTRIES
select * from zxyz into corresponding fields of table ITAB_FINAL
for all entries in itab
where matnr eq itab-matnr
and fromdate eq date1
and todate eq date2.
a® -
DECLARE @tableA table (Productid varchar(20),Product varchar(20),RateID int)
insert into @tableA values('1','Mobile',2);
insert into @tableA values('2','Chargers',4);
insert into @tableA values('3','Stand',6);
insert into @tableA values('4','Adapter',8);
insert into @tableA values('5','Cover',10);
insert into @tableA values('6','Protector',12);
--SELECT * FROM @tableA
DECLARE @tableB table (id varchar(20),RateID int,Rate int)
insert into @tableB values('1',2,200);
insert into @tableB values('2',4,40);
insert into @tableB values('3',6,60);
insert into @tableB values('4',8,80);
insert into @tableB values('5',10,10);
insert into @tableB values('6',12,15);
--SELECT * FROM @tableB
SELECT Product,Rate
FROM @tableA a
JOIN @tableB b ON a.RateID = b.RateID
Above is the sample query, where in execution plan it shows the Hash Match (inner Join). Now how do I change it to Nested Loop with out changing the query? help plzIs Hash Match(inner join) or Nested loop is better to have in the query?
That depends on the size of the tables, available indexes etc. The optimizer will (hopefully) make the best choice.
Above is the sample query, where in execution plan it shows the Hash Match (inner Join). Now how do I change it to Nested Loop with out changing the query?
The answer that you should leave that to the optimizer in most cases.
I see that the logical read for nested loop is higher than Hash Match.
But Hash Match tends to need more CPU. The best way to two compare two queries or plans is wallclock time.
On a big tables, how do we reduce the logical read?
Make sure that there are usable indexes.
Erland Sommarskog, SQL Server MVP, [email protected] -
Generally when does optimizer use nested loop and Hash joins ?
Version: 11.2.0.3, 10.2
Lets say I have a table called ORDER and ORDER_DETAIL.
ORDER_DETAIL is the child table of ORDERS .
This is what I understand about Nested Loop:
When we join ORDER AND ORDER_DETAIL tables oracle will form a 'nested loop' in which for each order_ID in ORDER table (outer loop), oracle will look for corresponding multiple ORDER_IDs in the ORDER_DETAIL table.
Is nested loop used when the driving table (ORDER in this case) is smaller than the child table (ORDER_DETAIL) ?
Is nested loop more likely to use Indexes in general ?
How will these two tables be joined using Hash joins ?
When is it ideal to use hash joins ?Your description of a nested loop is correct.
The overall rule is that Oracle will use the plan that it calculates to be, in general, fastest. That mostly means fewest I/O's, but there are various factors that adjust its calculation - e.g. it expects index blocks to be cached, multiple reads entries in an index may reference the same block, full scans get multiple blocks per I/O. It also has CPU cost calculations, but they generally only become significant with function / package calls or domain indexes (spatial, text, ...).
Nested loop with an index will require one indexed read of the child table per outer table row, so its I/O cost is roughly twice the number of rows expected to match the where clause conditions on the outer table.
A hash join reads the of the smaller table into a hash table then matches the rows from the larger table against the hash table, so its I/O cost is the cost of a full scan of each table (unless the smaller table is too big to fit in a single in-memory hash table). Hash joins generally don't use indexes - it doesn't use the index to look up each result. It can use an index as a data source, as a narrow version of the table or a way to identify the rows satisfying the other where clause conditions.
If you are processing the whole of both tables, Oracle is likely to use a hash join, and be very fast and efficient about it.
If your where clause restricts it to a just few rows from the parent table and a few corresponding rows from the child table, and you have an index Oracle is likely to use a nested loops solution.
If the tables are very small, either plan is efficient - you may be surprised by its choice.
Don't be worry about plans with full scans and hash joins - they are usually very fast and efficient. Often bad performance comes from having to do nested loop lookups for lots of rows. -
HELP with DO WHILE NESTED LOOPS PLEASE
I am trying to create a very basic Airline Reservation Sytem using JBuilder 3.
I am using JOptionPane boxes as the interface. The user has to input a number each time a menu appears and this takes him/her to the next or previous menu.
I tried using the switch statement but as there are many combinations and more choices on some menus than others it became very complex. I am now trying nested do while loops with out much success....Can anyone help????
A sample of my coded is included below... Thanks....
void go () throws IOException
int choice;
choice = printMenu();
do
if (choice == 1)
printMenu1();
else if (choice == 2)
printMenu2();
while (choice !=3);
int printMenu()
String usersChoice, output;
int choice;
do
output = "1 - Enter Airline/Flight/Passenger. \n";
output = output + "2 - Display Airline/Flight/Passenger. \n";
output = output + "3 - Exit. \n\n";
output = output + "Please enter a number:";
//get users selection
usersChoice = JOptionPane.showInputDialog(output);
//convert to integer
choice = Integer.parseInt(usersChoice);
}while(choice <1|| choice >3);
return choice;
}> void go () throws IOException
int choice;
choice = printMenu();
do
if (choice == 1)
printMenu1();
else if (choice == 2)
printMenu2();
while (choice !=3);
int printMenu()
String usersChoice, output;
int choice;
do
output = "1 - Enter Airline/Flight/Passenger. \n";
output = output + "2 - Display Airline/Flight/Passenger. \n";
output = output + "3 - Exit. \n\n";
output = output + "Please enter a number:";
//get users selection
usersChoice e = JOptionPane.showInputDialog(output);
//convert to integer
choice = Integer.parseInt(usersChoice);
}while(choice <1|| choice >3);
return choice;
}Your method printMenu() returns an integer, shouldn't you catch that into choice in your go() method? You probably have already done this, but I'd make a seperate method for each menu/submenu (perhaps even create a generic menu display method, pass in an array of strings/choices, prompt for a input and return an int, much like your printMenu() does).
kev -
Bulk collect with Nested loops
Hi I've a requirement like this
I need to pull request nos from table a(Master table)
For every request no I need to pull request details from table b(Detail Table)
For every request no I need to pull contact details from table c
For every request no I need to pull customer data table d and I need to create a flat file with that data so I'm using utl_file in normal query criterion because of nested loops it's taking lot of time so I want to use bulk collect with dynamic query option:
Sample code
=======
create or replace procedure test(region varchar2) as
type tablea_request_typ is table of varchar2(10);
tablea_data tablea_request_typ;
type tableb_request_typ is table of varchar2(1000);
tableb_data tableb_request_typ;
type tablec_request_typ is table of varchar2(1000);
tablec_data tablec_request_typ;
type tabled_request_typ is table of varchar2(1000);
tabled_data tabled_request_typ;
stmta varchar2(32000);
stmtb varchar2(32000);
stmtc varchar2(32000);
stmtd varchar2(32000);
rcura SYS_REFCURSOR;
rcurb SYS_REFCURSOR;
rcurc SYS_REFCURSOR;
rcurd SYS_REFCURSOR;
begin
stmta:='select request_no from tablea where :region'||'='NE';
stmtb:='select request_no||request_detail1||request_detail2 stringb from table b where :region'||'='NE';
stmtc:='select contact1||contact2||contact3||contact4 stringc from table c where :region'||'='NE';
stmtd:='select customer1||customer2||customer3||customer4 stringd from table c where :region'||'='NE';
OPEN rcura for stmta;
LOOP
FETCH rcura BULK COLLECT INTO request_no
LIMIT 1000;
FOR f in 1..request_no.count
LOOP
--Tableb
OPEN rcurb for stmtb USING substr(request_no(f),1,14);
LOOP
FETCH rcurb BULK COLLECT INTO tableb_data
for i in 1..tableb_data.count
LOOP
utl_file(...,tableb_data(i));
END LOOP;
EXIT WHEN rcurb%NOTFOUND;
END LOOP;
-- Tablec
OPEN rcurc for stmtc USING substr(request_no(f),1,14);
LOOP
FETCH rcurb BULK COLLECT INTO tablec_data
for i in 1..tablec_data.count
LOOP
utl_file(...,tablec_data(i));
END LOOP;
EXIT WHEN rcurc%NOTFOUND;
END LOOP;
-- Tabled
OPEN rcurd for stmtd USING substr(request_no(f),1,14);
LOOP
FETCH rcurd BULK COLLECT INTO tabled_data
for i in 1..tabled_data.count
LOOP
utl_file(...,tabled_data(i));
END LOOP;
EXIT WHEN rcurd%NOTFOUND;
END LOOP;
END LOOP;
EXIT WHEN rcura%NOTFOUND;
END LOOP;
exception
when other then
dbms_output.put_line(sqlerrm);
end;I 'm using code mentioned above but request nos are repeating as if it's an infinete loop ?for ex if request no is 222 It should run once but here it's running more than once?
How to pass bind parameters say in my case region?
Are there any alternate solutions to run it faster apart from using bulk collect?
Right now I'm using explicit cursor with for loop which is taking lot of time ?so is this better sol?
Thanks,
Mahender
Edited by: BluShadow on 24-Aug-2011 08:52
added {noformat}{noformat} tags. Please read {message:id=9360002} to learn to format your code/data yourself.Use Parameterized cursor :
CREATE OR REPLACE PROCEDURE test(region varchar2)
AS
type tablea_request_typ is table of varchar2(10);
type tableb_request_typ is table of varchar2(1000);
type tablec_request_typ is table of varchar2(1000);
type tabled_request_typ is table of varchar2(1000);
tablea_data tablea_request_typ;
tableb_data tableb_request_typ;
tablec_data tablec_request_typ;
tabled_data tabled_request_typ;
CURSOR rcura(v_region VARCHAR2(100))
IS
select request_no from tablea where region = v_region;
CURSOR rcurb(v_input VARCHAR2(100))
IS
select request_no||request_detail1||request_detail2 stringb from table b where request_num = v_input;
CURSOR rcurc(v_input VARCHAR2(100))
IS
select select contact1||contact2||contact3||contact4 stringc from table c where request_num = v_input;
CURSOR rcurd(v_input VARCHAR2(100))
IS
select select customer1||customer2||customer3||customer4 stringd from table c where request_num = v_input;
BEGIN
OPEN rcura('NE');
LOOP
FETCH rcura BULK COLLECT INTO request_no LIMIT 1000;
FOR f in 1..request_no.count
LOOP
--Tableb
OPEN rcurb(substr(request_no(f),1,14));
LOOP
FETCH rcurb BULK COLLECT INTO tableb_data
for i in 1..tableb_data.count
LOOP
utl_file(...,tableb_data(i));
END LOOP;
EXIT WHEN rcurb%NOTFOUND;
END LOOP;
-- Tablec
OPEN rcurc (substr(request_no(f),1,14));
LOOP
FETCH rcurb BULK COLLECT INTO tablec_data
for i in 1..tablec_data.count
LOOP
utl_file(...,tablec_data(i));
END LOOP;
EXIT WHEN rcurc%NOTFOUND;
END LOOP;
-- Tabled
OPEN rcurd ( substr(request_no(f),1,14) );
LOOP
FETCH rcurd BULK COLLECT INTO tabled_data
for i in 1..tabled_data.count
LOOP
utl_file(...,tabled_data(i));
END LOOP;
EXIT WHEN rcurd%NOTFOUND;
END LOOP;
END LOOP;
EXIT WHEN rcura%NOTFOUND;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(dbms_utility.format_error_backtrace);
END;
/Hope this helps. If not, post your table structures... -
Hi all, I have this annoying problem and I am looking for any suggestions.
I have 2 select boxes. One is for all available users, and second - for selected users (designated as admins). The list of all users is available in a collection (2 properties userId and userName displayed in the code below). The list of admins contains only userId (as strings).
I have no problem with populating selected users (admins) list, reusing pretty much the same logic below, but I cannot find a way to break out from the nested loop once the match is found, to avoid repetitions leading to incorrect display of results.
<select name=available>
<c:forEach items="${users}" var="user" varStatus="outer">
<c:forEach items="${adminIds}" var="adminId" varStatus="inner">
<c:if test="${user.userId!=adminId">
<option value="<c:out value="${user.userId}" />"><c:out value="${user.userFullName}"/></option>
</c:if>
</c:forEach>
</c:forEach>
</select>
<select name=selected>
<c:forEach items="${users}" var="user" varStatus="outer">
<c:forEach items="${adminIds}" var="adminId" varStatus="inner">
<c:if test="${user.userId==adminId">
<option value="<c:out value="${user.userId}" />"><c:out value="${user.userFullName}"/></option>
</c:if>
</c:forEach>
</c:forEach>
</select>Can anyone help, please? I am also restricted to JSP 1.2Double post: http://forum.java.sun.com/thread.jspa?threadID=707950&tstart=0
-
Use of Select stmts with Update stmts
Hi,
I want to execute the following update stmt...
UPDATE Test1_tab a
SET a.invpln_seq_no = (SELECT b.seq_no
FROM Test2_tab b
WHERE b.contract_id = a.contract_id
AND b.date_from = a.period_from_date)
WHERE a.invpln_seq_no != (SELECT b.seq_no
FROM Test2_tab b
WHERE b.contract_id = a.contract_id
AND b.date_from = a.period_from_date)
Here the problem is I have used 'SELECT b.seq_no FROM Test2_tab b WHERE b.contract_id = a.contract_id AND b.date_from = a.period_from_date' twice, one in the SET clause and the other one in WHERE clause...But it is the same SELECT stmt...So this same Select stmt runs twice when I run the whole update stmt I guess. Is there a way to do the above update, where it runs the SELECT stmt only once...
Any input is highly appreciated...
Thanks And Best Regards,
/Dinesh...Then you can use Merge statement instead.
try this
merge into test1_tab t1
using
(SELECT a.rowid,b.seq_no SEQ_DEST
FROM test1_tab a,test2_tab b
WHERE b.contract_id = a.contract_id
AND b.date_from = a.period_from_date
AND a.invpln_seq_no != b.seq_no) t2
ON (t1.rowid=t2.rowid)
WHEN MATCHED THEN
UPDATE SET t1.invpln_seq_no=t2.seq_destor if you are sure that sno is a unique key with out any duplicates and if you are going to perform one time manual update then you can use an undocumented hint /*+ bypass_ujvc */ to do this.
*DO NOT include this code if you are about to add it in a production procedure or a function
UPDATE /*+ bypass_ujvc */ (SELECT a.invpln_seq_no SEQ_SRC,b.seq_no SEQ_DEST FROM test1_tab a,test2_tab b
WHERE b.contract_id = a.contract_id
AND b.date_from = a.period_from_date
AND a.invpln_seq_no != b.seq_no)
SET SEQ_SRC = SEQ_DESTRegards,
Prazy -
Help with my nested loop...
this application allows a user to enter two int values, rows(height) and columns(length), that are used to create a box of asterisks.
my createBox() does not work correctly, only prints first line, loops infinitely, or doesnt loop. can someone help me out please?
import java.io.*;
public final class AsteriskBox
/****InstanceVariables****/
public static int row;
public static int col;
/****Constructor()****/
public AsteriskBox()
/****Access()'s****/
public void createBox(int x, int y)
if((x > 0) && (y > 0))
for(int i = x; i > 0; i--)
for(i = y; i > 0; i--)
System.out.print("*");
System.out.println();
} // createBox()
public void getInput() throws IOException
BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Enter a number of rows: ");
String inputString = input.readLine();
row = convertStringToInteger(inputString);
System.out.print("Enter a number of columns: ");
inputString = input.readLine();
col = convertStringToInteger(inputString);
} // getInput()
/****Helper()****/
private int convertStringToInteger(String s)
Integer intObject = Integer.valueOf(s);
return intObject.intValue();
} // convertStringToInt()
} // AsteriskBox class
import java.io.*;
public class AsteriskBoxUser
public static void main(String args[]) throws IOException
AsteriskBox box = new AsteriskBox();
System.out.println("This program creates a box made of asterisks with values given by you!");
System.out.println();
box.getInput();
box.createBox(AsteriskBox.row, AsteriskBox.col);
}You will never break out of the first loop because you are using the same index variable i for both of your nested loops. Use a different variable, say j for your inner loop.
Incidentally, your row and col variables are mislabeled. If you use the static modifier, they are class variables, not instance variables. This could cost you points on your assignment since it would imply that you don't know the difference. -
Too many nested loops in execution plan?
Hi,
i wonder about execution plan not indicating that access to some tables (for join) is in parallel.
Please see this example:
------------------------ snip ------------------------------------
drop table test_a1;
drop table test_a2;
drop table test_b;
drop table test_c;
drop table test_d;
create table test_a1 (
x number,
y number,
z number);
create unique index testa1_pk on test_a1 (x);
create table test_a2 (
x number,
y number,
z number);
create unique index testa2_pk on test_a2 (x);
create table test_b (
x number,
y number,
z number);
create unique index testb_pk on test_b (y);
create table test_c (
x number,
y number,
z number);
create unique index testc_pk on test_b (z);
create table test_d (
x number,
y number,
z number);
create unique index testd_pk on test_d (y);
select
a1.x a1_x,
a1.y a1_y,
a1.z a1_z,
a2.x a2_x,
a2.y a2_y,
a2.z a2_z,
b.x b_x,
b.y b_y,
b.z b_z,
c.x c_x,
c.y c_y,
c.z c_z,
d.x d_x,
d.y d_y,
d.z d_z
from test_a1 a1, test_a2 a2, test_b b, test_c c, test_d d
where a1.x = 100
and a2.x = 200
and b.y = a1.y
and c.z = b.z
and d.y = a1.y;
------------------------ snap ------------------------------------
The execution plan goes like this:
Select Stmt
nested loops
nested loops
nested loops
nested loops
table access
index
access predicate
a2.x = 200
table access
index
access predicate
a1.x = 100
table access
index
access predicate
d.y = a1.y
table access
index
access predicate
b.y = a1.y
table acess
index
acess predicate
c.z = b.z
Access to tables a1 and a2 is on the same level (in parallel - i guess).
However, why isn't access to table d and b on the same level?
Both depend on a1. So no need to execute one after the other (no inter-dependency).
Maybe i have just wrong expectation to the output of the execution plan(?!)
- many thanks!
best regards,
FrankPreservation of identation and spacing is invaluable when it comes to reading an explain plan.
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 195 | 2 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 1 | 195 | 2 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 1 | 156 | 0 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 1 | 117 | 0 (0)| 00:00:01 |
| 4 | NESTED LOOPS | | 1 | 78 | 0 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| TEST_A2 | 1 | 39 | 0 (0)| 00:00:01 |
|* 6 | INDEX UNIQUE SCAN | TESTA2_PK | 1 | | 0 (0)| 00:00:01 |
| 7 | TABLE ACCESS BY INDEX ROWID| TEST_A1 | 1 | 39 | 0 (0)| 00:00:01 |
|* 8 | INDEX UNIQUE SCAN | TESTA1_PK | 1 | | 0 (0)| 00:00:01 |
| 9 | TABLE ACCESS BY INDEX ROWID | TEST_D | 82 | 3198 | 0 (0)| 00:00:01 |
|* 10 | INDEX UNIQUE SCAN | TESTD_PK | 1 | | 0 (0)| 00:00:01 |
| 11 | TABLE ACCESS BY INDEX ROWID | TEST_B | 82 | 3198 | 0 (0)| 00:00:01 |
|* 12 | INDEX UNIQUE SCAN | TESTB_PK | 1 | | 0 (0)| 00:00:01 |
|* 13 | TABLE ACCESS FULL | TEST_C | 1 | 39 | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
6 - access("A2"."X"=200)
8 - access("A1"."X"=100)
10 - access("D"."Y"="A1"."Y")
12 - access("B"."Y"="A1"."Y")
13 - filter("C"."Z"="B"."Z")
Access to tables a1 and a2 is on the same level (in parallel - i guess).
Maybe i have just wrong expectation to the output of the execution plan(?!)You guess wrong, there's nothing parallel going on here.
Execution plan is a tree of parent-child operations.
For example, the NESTED LOOP at operation 4 has two children @ 5 and 7.
Both of these operations- 5 & 7 - have a single child operation.
The execution tree starts with operation 6, using the TESTA2_PK index to identify rows where A2.X=100.
From this list of rowids, we go to the table TEST_A2 operation 5.
The rows from operation five feed into the NESTED LOOP - operation 4.
For each of these rows, we go to TEST_A1 via the index TEST_A1_PK for rows where A1.X=100.
This is really a cartesian join because there's no join condition between the two tables.
etc, etc, etc
Three things in particular to point out.
Firstly, that nothing joins to A2. So there will be a cartesian product - i.e. for every row in the result set between the joined tables A1, B, C and D, these will be multiplied by the number of rows returned by the the A2 rowsource.
Secondly, when everything has got one or zero rows (or the optimizer thinks that it's one or zero rows), you can get very different plans from when there are known/thought to be more rows.
Both depend on a1. So no need to execute one after the other (no inter-dependency).Thirdly, in terms of isolated join operations (ignoring A2 and C for the moment), A1 cannot join to B and D at the same time, you can either join A1 to B and then join the result of that to D, or join A1 to D then B, which is what you've got in your plan (well, actually we have A2 joined to A1 then the result of that joined to D and then the result of that to B).
Edited by: Dom Brooks on Jul 6, 2011 4:07 PM
Corrected typo -
Hi Gurus,
I have the following problem :
There are 2 internal tables namely 1 &2.
Now there is a field in table 1 'AUGBL' which should be equal to field in Table2-BELNR.
Now if there is a match between TABLE1-AUGBL and TABLE2-BELNR,
so we move to table 2 to look for 'augbl' associated with 'belnr' in the same table.
Then I have to give the following logic -
If table2-belnr = table2-augbr,
Then do nothing.
Else.
Check, for the Augbl which will equal Belnr in a different position in table 2.
For ex:
Table1-AUGBL = 2.
So now we have to search a field when Table1-augbl = Table2-belnr = 2.
Now for this record in table2, there are 2 conditions:
When Table2-augbl = 2, (in which case we do nothing)
or,
Table2-augbl NE 2, or Table2-augbl = 3.
In the latter case, there will be another field in Table2-BELNR = 3. So we go to this record now and check for the associated AUGBL and do the same checks above.
We keep on continuing for this till the time we do not get the Table2-belnr = Table2-augbl in the end for the same record.
Now I want to achieve this without using nested loop.
Kindly help as I already have wated a lot of time.
Please let me know if there are any clarifications.Hey..
Before u get the data into two internal tables through SELECT stmts.
U can use JOIN instead of two SELECT stmts ..that wud fetch the data into single internal table..
and that may ease ur validations and further process..
try this out..
Thanks and Regards,
KC -
Performance problem with selecting records from BSEG and KONV
Hi,
I am having performance problem while selecting records from BSEG and KONV table. As these two tables have large amount of data , they are taking lot of time . Can anyone help me in improving the performance . Thanks in advance .
Regards,
PrashantHi,
Some steps to improve performance
SOME STEPS USED TO IMPROVE UR PERFORMANCE:
1. Avoid using SELECT...ENDSELECT... construct and use SELECT ... INTO TABLE.
2. Use WHERE clause in your SELECT statement to restrict the volume of data retrieved.
3. Design your Query to Use as much index fields as possible from left to right in your WHERE statement
4. Use FOR ALL ENTRIES in your SELECT statement to retrieve the matching records at one shot.
5. Avoid using nested SELECT statement SELECT within LOOPs.
6. Avoid using INTO CORRESPONDING FIELDS OF TABLE. Instead use INTO TABLE.
7. Avoid using SELECT * and Select only the required fields from the table.
8. Avoid nested loops when working with large internal tables.
9. Use assign instead of into in LOOPs for table types with large work areas
10. When in doubt call transaction SE30 and use the examples and check your code
11. Whenever using READ TABLE use BINARY SEARCH addition to speed up the search. Be sure to sort the internal table before binary search. This is a general thumb rule but typically if you are sure that the data in internal table is less than 200 entries you need not do SORT and use BINARY SEARCH since this is an overhead in performance.
12. Use "CHECK" instead of IF/ENDIF whenever possible.
13. Use "CASE" instead of IF/ENDIF whenever possible.
14. Use "MOVE" with individual variable/field moves instead of "MOVE-
CORRESPONDING" creates more coding but is more effcient.
Maybe you are looking for
-
Reviewing General Ledger Accounts - No line item detail
I have just started with a new company using SAP Business One for the first time. My frustration is when reviewing the general ledger line items that there is no detail coming through that lets me see what the amounts relate to. No Description or Ve
-
How do I download PDF files from Safari 5.0.5?
I am unable to open PDF files. All I get is a blank black screen. I now download PDFs into my computer from the internet (with mixed success), which is several steps more than has ever taken me in the past. Since finding this problem I have updated t
-
Lightroom 5.7 slide show crashing
I just moved Lightroom 5.7 to an I mac retina. Overtime I try to use the slide show module, it preps the slide show, then crashes once it begins the show. It was working well on my old iMac 27 in. Any suggestions? Running latest Yosemite operatin
-
Invalid Materialized view Oracle 8
Hi, I have a problem with a Invalid materialized view in Oracle 8. this view is in a .sql file. When the SQL file runs and the view is invalid the the loading is stopped. Does anyone know if it's possible in Oracle 10 that Oracle solves the problem (
-
9.0.115 problems
When trying to instal KB923789 (flash player security) I get 3 messages: 1. Gen Warn: Te version of Windows (XPpro/SP2/IE7) you have installed does not match the update you are trying to install. 2. Gen Warn Flash: The version of Macromedia Flash you