Need suggestions on my select statement.

Hello experts,
I am having with my select statement since it is running very slow. Normally, the itab it_vendor has records exceeding 7,000. So it loops 7, 000 times and I have 2 select statements which adds to the performance slowdown. Here it is guys:
*Select records records from BSIK and BSAK based on itab it_vendor
    LOOP AT it_vendor.
*Select records from BSIK
      SELECT belnr lifnr budat buzei gjahr sgtxt dmbtr
            shkzg saknr hkont zlspr FROM bsik
       INTO TABLE it_bsak
       FOR ALL ENTRIES IN it_vendor
       WHERE bukrs EQ p_bukrs
         AND budat LE p_keydt
         AND hkont IN so_saknr
         AND lifnr EQ it_vendor-lifnr
         AND umsks EQ space
         AND umskz EQ space.
*Select records from BSAK
      SELECT belnr lifnr budat buzei gjahr sgtxt dmbtr
             shkzg saknr hkont zlspr FROM bsak
        APPENDING TABLE it_bsak
        FOR ALL ENTRIES IN it_vendor
        WHERE bukrs EQ p_bukrs
          AND augdt GT p_keydt
          AND budat LE p_keydt
          AND hkont IN so_saknr
          AND lifnr EQ it_vendor-lifnr
          AND umsks EQ space
          AND umskz EQ space.
    ENDLOOP.

Since you are using the FOR ALL ENTRIES extension of the select statement, you should not have these inside the loop.  What it is correctly doing is getting all the records for all the vendors every time thru the loop.  The FOR ALL ENTRIES will get all the records for all of the vendors in one shot, no need to LOOP at the internal table.  Make sure that you change your where clauses to how I have them below.
* Get the IT_VENDOR itab here
<b> check not it_vendor[] is initial.
sort it_vendor ascending by lifnr .</b>
*Select records from BSIK
SELECT belnr lifnr budat buzei gjahr sgtxt dmbtr
shkzg saknr hkont zlspr FROM bsik
INTO TABLE it_bsak
FOR ALL ENTRIES IN it_vendor
WHERE<b> lifnr EQ it_vendor-lifnr</b>
and  bukrs EQ p_bukrs
AND budat LE p_keydt
AND hkont IN so_saknr
AND umsks EQ space
AND umskz EQ space.
*Select records from BSAK
SELECT belnr lifnr budat buzei gjahr sgtxt dmbtr
shkzg saknr hkont zlspr FROM bsak
APPENDING TABLE it_bsak
FOR ALL ENTRIES IN it_vendor
WHERE  <b>lifnr EQ it_vendor-lifnr</b>
and bukrs EQ p_bukrs
AND augdt GT p_keydt
AND budat LE p_keydt
AND hkont IN so_saknr
AND umsks EQ space
AND umskz EQ space.
Regards,
Rich Heilman

Similar Messages

  • Cursor - Suggestions for Dynamic select statements

    Hey,
    Am trying to define a cursor like this -
    cursor c1 is
    select table_name from dba_tables INTERSECT select table_name from dba_tables@SOME_DBLINKMy need is to pass this dblink as IN parameter to the procedure and use it in the select statement for cursor. How can I do this?
    Any suggestion is highly appreciated. Thanks!

    Well that was meant to be my point. If you had two, you wouldn't (I hope) call the second one "c2" - you would be forced to think about what it represented, and name it "c_order_history" or something. Sticking "1" on the end does not make an extensible naming convention for cursors any more than it does for variables, procedures, tables or anything else, and so the "1" in "c1" is redundant because there will never be a c2.

  • Need help with a SELECT statement

    Dear forumers,
    I'm a newbie in ABAP and I'm trying to figure out what the following line means (in bold letters):-
    SELECT SINGLE STAT2 FROM PA0000 INTO PA0000-STAT2
    WHERE PERNR = ITAB_DATA-STAFF_NO.
    Does this mean that the single data from the STAT2 field is selected and then placed back into the same field (doesn't quite makes sense, right)?
    Please help and many thanks in advance!

    SELECT SINGLE STAT2 FROM PA0000 INTO PA0000-STAT2
    WHERE PERNR = ITAB_DATA-STAFF_NO.
    Does this mean that the single data from the STAT2 field is selected and then placed back into the same field (doesn't quite makes sense, right)?
    it sense very much right.
    have you heared about table buffer concept?
    here this concept exactly comes.
    try with this below code:
    TABLES:pa0000."tables must be decleare
    SELECT SINGLE STAT2 FROM PA0000 INTO PA0000-STAT2.
      write:PA0000-STAT2.
    and since you are using select single than you should be use much key as much possible for getting accurate data.other wise system will pick first data which match your where condition.
    Amit.

  • Help need in select statement?

    Hi experts...
    I have an internal table with 2 records...i_mara with fields matnr and ersda
    matnr   ersda
    2345    01/26/2007
    3445    02/05/2007
    i need to write a select statement to collect all MBLNR from MKPF table where BUDAT is in between 01/26/2007 and 02/05/2007..
    Thanx
    Giri

    HI vasanth and rich...
    thanks for replies...
    vasanth i am getting error with the statement.. so i assigned 6 points to u..
    DATA: RA_BUDAT RANGE OF BUDAT WITH HEADER LINE.
    Unable to interpret range.... Possible error with spelling or comma
    Rich: ur solution worked good....
    is there any possible simplest way... or this is the best way includes performance?
    Regards
    Giri

  • Select statement in if/else condition

    Hi i need to write a select statement in the if condition in pl/sql how can i write this
    example :
    if field_name not in (select statement) then
    Is this type of if condition is possible in pl/sql?
    thanks in advance for help.

    Qwerty wrote:
    here pick a job example salesman for ename ward, now i want to compare this job that is "salesman" with all the jobs which are before it. that is clerk in line 1 and salesman in line 2Define "before it". There is no order in relational tables. Only ORDER BY means ordered sets. Therefore there is no before/after without ORDER BY. Assuming ORDER BY empno, job count of same job title before empno:
    select  ename,
            job,
            count(*) over(partition by job order by empno) - 1 same_job_count_before_empno
      from  emp
    ENAME      JOB       SAME_JOB_COUNT_BEFORE_EMPNO
    SCOTT      ANALYST                             0
    FORD       ANALYST                             1
    SMITH      CLERK                               0
    ADAMS      CLERK                               1
    JAMES      CLERK                               2
    MILLER     CLERK                               3
    JONES      MANAGER                             0
    BLAKE      MANAGER                             1
    CLARK      MANAGER                             2
    KING       PRESIDENT                           0
    ALLEN      SALESMAN                            0
    ENAME      JOB       SAME_JOB_COUNT_BEFORE_EMPNO
    WARD       SALESMAN                            1
    MARTIN     SALESMAN                            2
    TURNER     SALESMAN                            3
    14 rows selected.To find job count of same job title as Ward has before Ward (by empno):
    SELECT  same_job_count_before_empno
      FROM  (
             select  ename,
                     count(*) over(partition by job order by empno) - 1 same_job_count_before_empno
               from  emp
      WHERE ename = 'WARD'
    SAME_JOB_COUNT_BEFORE_EMPNO
                              1SY.

  • MySql select statement in jsp page

    Ok,
    I need help with this select statement.
    <%
    // Determine what option is set to.
    if(option == null || "".equals(option) || "Verify1".equals(option)){
      if("Verify1".equals(option)){
      // Retrive query specific to submitted form.
      try {
          Class.forName("org.gjt.mm.mysql.Driver").newInstance();
          conn = DriverManager.getConnection("jdbc:mysql://" + mysql_host + ":3306/" + mysql_database, mysql_login, mysql_password);
          st = conn.createStatement();
          rs= st.executeQuery("SELECT organization FROM tblevent_approval WHERE MAX (keyid))";
          // Get query results.
          while(rs.next()){
            organization = rs.getString("organization");
      finally {
            if(rs != null){
              rs.close();
            if(st != null){
              st.close()
            if(conn != null){
              conn.close();
    %>THE ERROR I AM GETTING
    Syntax error, insert ")" to complete Expression

    This line has the closing bracket inside the closing quote for the statement...
    >       rs= st.executeQuery("SELECT organization FROM tblevent_approval WHERE MAX (keyid))";It should be:
    rs= st.executeQuery("SELECT organization FROM tblevent_approval WHERE MAX (keyid)");A simple typo ;-)
    Hope this helps...

  • Restricting records in the select statement

    Hi,
    I have one requirement where in i need to restrict my select statement to get only 250 records in the output table.
    Here are the select statements i am using....
        SELECT a~ebeln
                     a~lifnr
                     a~ekgrp
                     b~werks
                     b~matnr
               FROM ekko AS a INNER JOIN ekpo AS b
               ON aebeln = bebeln
               INTO TABLE i_ekko
               WHERE a~ebeln IN r_ebeln AND
                            a~lifnr IN r_lifnr AND
                            a~ekgrp IN r_ekgrp AND
                            b~werks IN r_ewerk AND
                            b~matnr IN r_matnr AND
                            a~bstyp = 'F'.
                IF NOT i_ekko[] IS INITIAL.
    Select Vendor name
               SELECT lifnr
                            name1
                      FROM lfa1
                      INTO TABLE i_lfa1
                      FOR ALL ENTRIES IN i_ekko
                      WHERE lifnr = i_ekko-lifnr.      
    Here if i restrict the first select statement to get only 250 records then i am not getting the desired record in the second select statement.
    Could anyone let me know how to write the select statement.
    Regards,
    Ramesh

    restric the first select using up to 250.
    sort the i_ekko by vendor name.
    ex: sort i_ekko by lifnr.
    use second select statement here.

  • Facing problem in select statement dump DBIF_RSQL_INVALID_RSQL CX_SY_OPEN_S

    Hi Experts,
    I  am facing the problem in the select statement where it giving the short dump
    DBIF_RSQL_INVALID_RSQL CX_SY_OPEN_S.
    i have searched many forms, but i found that the select option s_matnr have the limitaion 2000 entreis, but i am passing same s_matnr to other select statement with more than 2000 entries but it is not giving me any short dump.
    but i am facing problem with only one select statement where if i  pass select option s_matnr more than 1500 entris also giving short dump.
    my select statement is
    SELECT * FROM bsim                                       
             INTO CORRESPONDING FIELDS OF TABLE g_t_bsim_lean  
               FOR ALL ENTRIES IN t_bwkey   WHERE  bwkey = t_bwkey-bwkey
                                            AND    matnr IN matnr
                                            AND    bwtar IN bwtar
                                            AND    budat >= datum-low.
    in the internal table g_t_bsim_lean internal table contain all the fields of the table bsim with 2 fields from other table.
    Please let me know whether i need to change the select statement or any other solution for this.
    Regards,
    udupi

    my select query is like this:
    DATA: BEGIN OF t_bwkey OCCURS 0,                          "184465
              bwkey LIKE bsim-bwkey,                            "184465
            END OF t_bwkey.                                     "184465
      LOOP AT g_t_organ          WHERE  keytype  =  c_bwkey.
        MOVE g_t_organ-bwkey     TO  t_bwkey-bwkey.
        COLLECT t_bwkey.                                        "184465
      ENDLOOP.                                                  "184465
      READ TABLE t_bwkey INDEX 1.                               "184465
      CHECK sy-subrc = 0.                                       "184465
      SELECT * FROM bsim                                        "n443935
             INTO CORRESPONDING FIELDS OF TABLE g_t_bsim_lean   "n443935
               FOR ALL ENTRIES IN t_bwkey   WHERE  bwkey = t_bwkey-bwkey
                                            AND    matnr IN matnr
                                            AND    bwtar IN bwtar
                                            AND    budat >= datum-low.

  • I need help with a SELECT query - help!

    Hello, I need help with a select statement.
    I have a table with 2 fields as shown below
    Name | Type
    John | 1
    John | 2
    John | 3
    Paul | 1
    Paul | 2
    Paul | 3
    Mark | 1
    Mark | 2
    I need a query that returns everything where the name has type 1 or 2 but not type 3. So in the example above the qery should bring back all the "Mark" records.
    Thanks,
    Ian

    Or, if the types are sequential from 1 upwards you could simply do:-
    SQL> create table t as
      2  select 'John' as name, 1 as type from dual union
      3  select 'John',2 from dual union
      4  select 'John',3 from dual union
      5  select 'Paul',1 from dual union
      6  select 'Paul',2 from dual union
      7  select 'Paul',3 from dual union
      8  select 'Paul',4 from dual union
      9  select 'Mark',1 from dual union
    10  select 'Mark',2 from dual;
    Table created.
    SQL> select name
      2  from t
      3  group by name
      4  having count(*) <= 2;
    NAME
    Mark
    SQL>Or another alternative if they aren't sequential:
    SQL> ed
    Wrote file afiedt.buf
      1  select name from (
      2    select name, max(type) t
      3    from t
      4    group by name
      5    )
      6* where t < 3
    SQL> /
    NAME
    Mark
    SQL>Message was edited by:
    blushadow

  • Concatenation in select statement ?

    I need some help from you people. I need to write a select statement, and in the WHERE condition I need to compare the table field with concatenation of two fields ( say F1 and F2) which I already determined.
    And I need to do this for say 1 million set of field combinations of F1 and F2. I can write a loop before the select statement and find all the concatenations of F1 and F2  . But I want to make it less time consuming.
    Please let me know how to do this.
    Thanks

    Why not use a range.  Add all of the combinations to the range and then use the range in your Select statement.
    Something like this.
    report zrich_0003.
    TYPes: begin of trange,
           value(30) type c,
           end of trange.
    data:  xrange type trange.      
    ranges: r_range for xrange.
    r_range-sign   = 'I'.
    r_range-option = 'EQ'.
    concatanate 'A' 'B' into r_range-low.
    append r_range.
    r_range-sign   = 'I'.
    r_range-option = 'EQ'.
    concatanate 'C' 'D' into r_range-low.
    append r_range.
    r_range-sign   = 'I'.
    r_range-option = 'EQ'.
    concatanate 'E' 'F' into r_range-low.
    append r_range.
    Select * into itable from ztable
             where some_field in r_range.
    Regards,
    Rich Heilman

  • Order of records in o/p  of select statement

    Hi all
    I have doubt regarding the select statement. I need to execute a select statement (with out any order ie with out "order by") which searchs a table. I want to know whether in any
    circumstances the order of the records of the select statement is different.
    sample select statement which I need to execute
    select emp_no from emplyees where designation = 'programer'
    in one word order of records of in the o/p of a select statement will be unique ?
    (provided same query same, table,)
    can u plz quote the link to get more information
    regards
    Renjith

    Hi,
    YES, you can
    Do Order By Without Using Order By Clause
    in your select statement.
    I assume that you have unique data ( e.g. emp_no in Emp Table ) and you you do not want to use Order by clause.
    Solution 1:
    Select Emp_no
    from Emp
    group by Emp_no;
    the o/p will be in the sorted in the Ascending order.
    Solution 2: ( Only for columns holding Numeric Values )
    Select Emp_No
    From Emp
    Where 99999 - Emp_no in ( Select 99999 - Emp_no from Emp );
    Again this will sort the result will be in the Descending Order. you can use any big number instead of 99999 but it should be greater than Emp_no value.
    Note: You Should only use this method on not very large tables coz of performance issue.
    Hope this will solve your problem.
    Any Comment on this, any body.
    Thanks and Regards
    There is always a solution to the problem, And if there is no solution of a problem then problem is not a problem.

  • ABAP Joins or Separate select statements?

    Hi,
       I am working on a simulation of an implementation project as part of my in-house training programme. I am a rookie to ABAP and am now doing a print program that requires master data selection from tables ANLZ, LFA1, MSEG and CSKT, and also related line items from ANLA.
      As a former Java/.NET programmer I used to play with database joins. Now since ABAP joins are much simpler to use, I take the best advantage of them. For instance, in this particular program, I fetched related data from ANLZ, ANLA, LFA1 and CSKT into one internal table using a single select statement with ABAP joins.
      Now I happen to hear (in bits and pieces) that one should limit the use of nested joins to a certain extend. Instead, for the above program, I was advised to use 4 internal tables and to select data separately using FOR ALL ENTRIES. But in this case, we need to use 4 select statements, which means 4 database operations in place of just one.
      From ABAP documentation, I read that we can link upto 24 tables in a single select statement using ABAP joins.
    Can anyone clarify more about this? Which is high on performance?
    Using a single select statement using joins
    or
    Using multiple selects -- FOR ALL ENTRIES?
    Thanks and regards,
    Arun

    Hi Arun,
    you keep reading in these forums that FOR ALL ENTRIES is more performant, but this is simply not true.
    In your example, joining four tables for one DB operation is better than storing a lot of redundant data in internal tables just to perform four separate DB operations using FOR ALL ENTRIES.
    Make sure you join the tables correctly, i.e. link dependent tables giving their full primary key.
    FOR ALL ENTRIES can be used where effective join statements are not possible, e.g. when cluster tables are involved (like BSEG).
    Cheers
    Thomas
    Edit: check this out too: Inner Join or For All Entries

  • SELECT statement throwing error in Pro*C

    Hi All,
    I have a Pro*C report that I am creating and the selmacro in the report has a SQL statement like
    EXEC SQL DECLARE cursor_000 CURSOR FOR
    SELECT (select 'Y' from dual)
    FROM spriden
    WHERE spriden_change_ind IS NULL;
    Now this throws me an error saying :
    Syntax error at line 538, column 16, file tzrages.pc:
    Error at line 538, column 16 in file tzrages.pc
    SELECT (select 'Y' from dual)
    ...............1
    PCC-S-02201, Encountered the symbol "'Y'" when expecting one of the following:
    ( ) * + - / . @ | at, day, hour, minute, month, second, year,
    Syntax error at line 1954, column 16, file tzrages.pc:
    Error at line 1954, column 16 in file tzrages.pc
    FROM DUAL;
    My Observation is that when I put a "select" statement in the select clause of the SQL, it throws the error. However if I write the query with out the "SELECT" in the select clause, it works just fine.
    Any clues on this. I need to have multiple "Select" statements in the SELECT clause, and cannot do without it.

    Thanks Satrap and Billy for replying to the post. Appreciate it.
    What the query actually looks like is
    SELECT college_name
    ,college_location
    ,student_category
    ,sum(30_day_balance)
    ,sum(60_day_balance)
    ,sum(90_day_balance)
    FROM ( SELECT college_name
    ,college_location
    , NVL ( (SELECT DECODE (substr(A.company_id,1,1)
    ,'V','Bad Debt'
    ,'D','Active SAC'
    FROM company_table A
    ,collections_table C
    WHERE C.student_id = B.student_id
    AND C.collector_id = A.collector_id
    ), 'Active'
    ) STUDENT_CATEGORY
    , ( SELECT NVL ( SUM ( D.balance),0)
    FROM balance_table D
    WHERE D.balance_date BETWEEN to_date(:report_run_date) - 30 AND to_date (:report_run_date)
    AND D.collection_code = :collection_code
    AND D.student_id = B.student_id
    ) 30_DAY_BALANCE
    , ( SELECT NVL ( SUM ( D.balance),0)
    FROM balance_table D
         WHERE D.balance_date BETWEEN to_date(:report_run_date) - 60 AND ( to_date (:report_run_date) - 30 + 1)
    AND D.collection_code = :collection_code
         AND D.student_id = B.student_id
         ) 60_DAY_BALANCE
         , ( SELECT NVL ( SUM ( D.balance),0)
    FROM balance_table D
         WHERE D.balance_date BETWEEN to_date(:report_run_date) - 90 AND ( to_date (:report_run_date) - 60 + 1)
    AND D.collection_code = :collection_code
         AND D.student_id = B.student_id
         ) 90_DAY_BALANCE
         FROM student_table B
         WHERE B.student_id IN ( SELECT E.student_id
         FROM collections_table E
         WHERE E.collection_code = :collection_code
         GROUP BY E.student_id
         HAVIN SUM(E.balance) BETWEEN :min_balance AND :max_balance
    GROUP BY college_name,college_location,student_category
    Please note that anything with a ':' as a prefix (:collection_code,:max_balance) is a parameter for the Query
    When I run the query in toad, it works fine, but when I compile it in Pro*C it throws an error as mentioned in the previous post.

  • Select statement issue--urgent

    Hi All,
    i need information regarding below select statement.
    SELECT ebelp
               ebeln
               vgabe
               SUM( menge ) AS menge
               shkzg
          FROM ekbe
          INTO TABLE t_ekbe
         WHERE ebeln EQ t_documents-ebeln
           AND ebelp EQ t_documents-ebelp
           AND ( vgabe EQ c_1
            OR   vgabe EQ c_2 )
         GROUP by ebelp ebeln vgabe shkzg.
    I am working 4.5b version now.All the data declartion is correct.This select statement goes to Dump .when ever data is selected before the select statment.
    let me any other choices to do same.its urgent.
    Thanks,
    Arnald

    Hi...Arnald..
    Try this way... Runtime error may be bcoz of the Order of fields....
    types : begin of st_ekbe,
                 EBELN TYPE EKBE-EBELN,
                 EBELPTYPE EKBE-EBELP,
                 VGABE TYPE EKBE-VGABE,
                 SHKZG TYPE EKBE-SHKZG,
                 MENGE TYPE EKBE-MENGE,
               end of st_ekbe.
    DATA: T_EKBE TYPE TABLE OF ST_EKBE.
    SELECT
    ebeln
    ebelp
    vgabe
    shkzg
    SUM( menge )
    FROM ekbe
    INTO TABLE t_ekbe
    WHERE ebeln EQ t_documents-ebeln
    AND ebelp EQ t_documents-ebelp
    AND ( vgabe EQ c_1
    OR vgabe EQ c_2 )
    GROUP by  ebeln  ebelp vgabe shkzg.
    <b>Reward if Helpful</b>

  • How to generate mutiple Results for multiple SQL SELECT statements

    I just downloaded SQL Developer 1.5 and play around with it.
    On a daily basis, I need run 4 different SELECT statements and dump the output of each SELECT into an CSV file and send to some users. In SQL Navigator, I can select all 4 SELECT statements and execute them at same time. Once all complete, I will see 4 tabs, each represent the output for each SELECT statement. Then I can export output from each tab into a CSV file.
    It seems that SQL Developer 1.5 still can't run mutiple SELECT statements and display the output of each statement into a separate "Results" tab so that I can export the data into multiple CSV files easily.
    Right now, I have to hightlight each SELECT statement and press F9, then export the output data into CSV.
    I wish I can execute 4 SELECT statements all in once on SQL Developer and get all the output data and export 4 times into 4 CSV files.
    Thanks!
    Kevin

    How about doing it as a set of tabs within the Resuls Tab?
    So you would have your Top row of tabs:
    Results, Script Output, Explain, AutoTrace, DBMS Output and OWA Output
    Then When you have the Results tab selected you could have a row of tabs beneath the Top row with one tab for each result set. Switching between result sets should switch which section of SQL is highlighted as the "Current SQL".
    A similar mechinism could be employed for each of the top level tabs where it makes sense to have multiple output tabs.
    A further refinement of this feature might be to allow the result tabs to be dockable within the parent tab so that multiple result tabs can be viewed simultaneously. This way 2 or more explain plans (for example) could be visually compared without requiring the code to exist in two separate code panes.

Maybe you are looking for

  • How do you get Openoffice to look more Gnome-like?

    When you install OOo (version 2.2.1-3), some of the output in the termial says use "export OOO_FORCE_DESKTOP=kde" or use "export OOO_FORCE_DESKTOP=gnome" to preset your desired look, or put it in /etc/profile, rc.local, or ~/.bashrc. I have tried cop

  • TEHO not working

    Hi, I have a problem with TEHO and SIPTRUNK. The scenario is an AVAYA using  a SIP Trunk connected to a CME connected to the PSTN over QSIG E1. Calls from the AVAYA to any IP Phone on the CME are working fine. Calls from the IP Phones on the CME to t

  • Data Warehouse Archive logging questions

    Hi all, I'd like some opinions/advice on archive logging and OWB 10.2 with a 10.2 database. Do you use archive logging on your non-production OWB instances? I have a development system that only has "on demand" backups done and the archive logs fill

  • 9i New feautures for devlopers

    Hello All, Does anyone have any idea where can i find the pdf/html for the 9i new features for developers. I would always come across it but being a DBA never downloaded it and now i have been asked by the development team. Cheers! Ali.

  • Linked policies not copying to sysvol\domain\policies?

    This problem started recently, and I have not been able to find any resolutions online. This is a relatively new installation of Server 2012 R2 on a small network of about 25 PCs. I was able to set up a number of GPOs without issue, but all of a sudd