USING INDEXES DEFINED ON TABLES IN SELECT STATEMENTS

Hi there,
I would like to ask a simple question, how i can use indexes defined on certain columns on one of my database tables in a select clause:
table name: B2TECDOC
schema name: AWDBT1M4
indexes defined: AWDBT1M4.B2TECDOC_B2TECDOCKEY
index AWDBT1M4.B2TECDOC_B2TECDOCKEY is defined on columns:
documentname
documenttype
organizationid
organizationtype
revision
sequence
versionnumbercould you help me construct a select sql using this index?
another question is will using this index in my select clause increase the overall query performance?
thanks
rohan

A query like
SELECT *
  FROM AWDBT1M4.B2TECDOC
WHERE documentname = <<some value>>
   AND documenttype = <<some value>
   AND organizationid = <<some value>>
   AND organizationtype = <<some value>>
   AND revision = <<some value>>
   AND sequence = <<some value>>
   AND versionnumber = <<some value>>should use the index.
An index is a more efficient way to access a relatively small fraction of the rows in the table. A table scan will be more efficient if you are trying to access a relatively large fraction of the rows in the table. Exactly what "small fraction" and "large fraction" means will depend on a variety of factors that the cost-based optimizer (CBO) attempts to evaluate in determing the query plan.
Justin

Similar Messages

  • Using plsql tables in select statement of report query

    Hi
    Anyone have experience to use plsql table to select statement to create a report. In otherwords, How to run report using flat file (xx.txt) information like 10 records in flat files and use this 10 records to the report and run pdf files.
    thanks in advance
    suresh

    hi,
    u can use the utl_file package to do that using a ref cursor query in the data model and u can have this code to read data from a flat file
    declare
    ur_file utl_file.file_type;
    my_result varchar2(250);
    begin
    ur_file := UTL_FILE.FOPEN ('&directory', '&filename', 'r') ;
    utl_file.get_line(ur_file, my_result);
    dbms_output.put_line(my_result);
    utl_file.fclose(ur_file);
    end;
    make sure u have an entry in ur init.ora saying that your
    utl_file_dir = '\your directory where ur files reside'
    cheers!
    [email protected]

  • Best way of Using Index on a Table.

    I am trying to understand the phenomena of using INDEX on a Table
    need some guidance!!!
    Let us take this scenario
    I have a table "MYRECORD" which has 4 attributes(or coulombs)
    1. "STATE" (varchar) // this can have 49 different values like newyork, dehli etc
    2. "YEAR" //a year like 2007
    3. "MONTH" //a month like JAN,FEB etc
    4. "CAT" (int) // type(category) of data represented by values 0 to 40
    with a PRIMARY KEY(STATE,YEAR,MONTH,CAT)
    now i will create index
    1. INX_myrecord (STATE,YEAR,MONTH) on table MYRECORD
    so now my question is
    1. what is the effect on performance of DB it makes?
    2. when I use a query
    SELECT * FROM MYRECORD WHERE STATE="dehli" AND YEAR=2007 AND MONTH="JAN";
    how will it get processed if index is created and not created.
    3. how can I refer a index by name in a query if so possible?
    Cheers,
    UD
    Message was edited by:
    UDAY

    You have edited your post. Now you have a primary key consisting of state, year, month and cat which makes an index on state, year and month useless as the already existing primary key can provide for retrieval of rows by index. If you don't have other columns - or just few other not being large varchar2 columns - you should have created the table as an IOT (Index Organized Table - avoiding to have separate table and index containing - nearly - the same data) in the first place.
    As a primary key by definition can contain only unique non null values, a query like SELECT * FROM MYRECORD WHERE STATE='dehli' AND YEAR=2007 AND MONTH='JAN' cannot give you more than the number of distinct cat values (0 .. 40) + 1 (if cat can be null - presumed one/some of the corresponding state, year and month is not null)
    The information processing depends principally of the query, the mere presence of an index does not make sure it will be used. If an index is used it means the index will be searched first then the table rows will be accessed by rowids contained in the index (usually a single row or a range of rows - a rather small number of them - is retrieved this way, your select for example). Submitting something like SELECT * FROM MYRECORD WHERE YEAR=2007 AND cat=33 would most likely produce a full table scan of myrecord table ignoring the primary key.
    Regards
    Etbin

  • What is proper syntax for using PassBack in a Report page select statement

    In my select statement I reference the passBack function using:
    onClick="javascript:passBack()"
    In my Page Header I (Typically) define the passBack function as:
    <script language="JavaScript" type="text/javascript">
    function passBack(passVal1,passVal2) {
    opener.document.getElementById("P2000_CABLE_ID").value = passVal1;
    opener.document.getElementById("P2000_CABLE_LABEL").value = passVal2;
    window.opener.location.reload(true);
    </script>
    But I have never used the passBack function from the Select statement before and cannot happen onto the proper syntax.
    The Page Items I want to pass are:
    1. :P2004_CABLE_ID
    2. :P2004_LABEL
    I want to pass them to:
    1. :P2000_CABLE_ID
    2. :P2000_CABLE_LABEL
    Can someone please help me out... Again?
    Thanks- Gary

    Let's assume column XYZ has both numbers (1), and letters (any alphabet).
    I have a case statement on SQL to turn any value that's not 1 into 0, then I am getting a sum of that column.
    I am also grouping by Row A, B etc to get aggregated sum of column XYZ for those group.
    Now on Crystal Reports function, I need to sum up values under column XYZ for all the groups.
    If I try using sum function like below, I get an error stating:
    "A number field or currency amount field is required here"
    (sum({Command.XYZ}))
    So I thought if I can use a case statement to change the non-numbers to 0 prior to sum that will probably resolve it. But I could not get the below case statement to work either (Error: A string is required). 
    SELECT {Command.XYZ}
       Case 1:
          1 
       Default:
          0;

  • Using temporary tablespace for sort in select statement without spacifying

    how can i use some particular temporary tablespace in select statement for sording without allocating any temporary tablespace to that user

    Try to set for the current session the in memory sorting space to 0 before running your query:
    SQL> alter session set sort_area_size = 0;The query should use the temporary tablespace.
    Message was edited by:
    Pierre Forstmann

  • Using Nested Table in Select Statement

    Hi all ,
    Can i use the PL/SQL nested table or Varray
    in the select statement as a normal table joined with other database tables.
    i.e.
    I have a nested table NT_1 in PL/SQL proc
    i have to use this NT_1 in the select statement as
    select xxx from
    tab_1,
    tab_2,
    NT_1
    where
    < some conditional joins >.
    Please help me in this regard.
    regds
    Dhananjaya.H

    you can not use a varray as part of a SQL Statement in order to build joins.
    Can you explain better what do you want to do ?
    Joel P�rez

  • HOW CAN I  USE MULTIPLE INNERJOINS IN A SINGLE SELECT STATEMENT?

    HI,
    I AM SHABEER AHMED,
    I AM GETTING AN ERROR WHILE I ATTEMPT TO EXECUTE A SELECT STATEMENT WITH MULTIPLE INNER JOINS . BECOZ I WANT TO FETCH ITEM DATA, PARTNER DATA  BASED ON HEADER DATA .
    THEN OF COURSE I HAVE FETCH DATA FROM VBAK VBAP VBKD SO LZ SEND ME THE SOLUTION.
    BYE

    Hi,
    1.Just see this:
    SELECT * INTO CORRESPONDING FIELD OF TABLE itab
    FROM t1 INNER JOIN t2 ON t1f4 EQ t2f4
    INNER JOIN t3 ON t2f5 EQ t3f5 AND
    t2f6 EQ t3f6 AND
    t2f7 EQ t3f7.
    2.But better to use for all entries.It increases the performance.
    FOR ALL ENTRIES
    Tabular Conditions
    The WHERE clause of the SELECT statement has a special variant that allows you to derive conditions from the lines and columns of an internal table:
    SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond> ...
    <cond> may be formulated as described above. If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table. For each line, the system selects the lines from the database table that satisfy the condition. The result set of the SELECT statement is the union of the individual selections for each line of the internal table. Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.
    The internal table <itab> must have a structured line type, and each field that occurs in the condition <cond> must be compatible with the column of the database with which it is compared. Do not use the operators LIKE, BETWEEN, and IN in comparisons using internal table fields. You may not use the ORDER BY clause in the same SELECT statement.
    You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.
    Example for ALL ENTRIES
    DATA: TAB_SPFLI TYPE TABLE OF SPFLI,
    TAB_SFLIGHT TYPE SORTED TABLE OF SFLIGHT
    WITH UNIQUE KEY TABLE LINE,
    WA LIKE LINE OF TAB_SFLIGHT.
    SELECT CARRID CONNID
    INTO CORRESPONDING FIELDS OF TABLE TAB_SPFLI
    FROM SPFLI
    WHERE CITYFROM = 'NEW YORK'.
    SELECT CARRID CONNID FLDATE
    INTO CORRESPONDING FIELDS OF TABLE TAB_SFLIGHT
    FROM SFLIGHT
    FOR ALL ENTRIES IN TAB_SPFLI
    WHERE CARRID = TAB_SPFLI-CARRID AND
    CONNID = TAB_SPFLI-CONNID.
    LOOP AT TAB_SFLIGHT INTO WA.
    AT NEW CONNID.
    WRITE: / WA-CARRID, WA-CONNID.
    ENDAT.
    WRITE: / WA-FLDATE.
    ENDLOOP.
    INNER JOINS
    In a relational database, you normally need to read data simultaneously from more than one database table into an application program. You can read from more than one table in a single SELECT statement, such that the data in the tables all has to meet the same conditions, using the following join expression:
    SELECT...
    FROM <tab> INNER JOIN <dbtab> AS <alias> ON <cond> <options>
    where <dbtab> is a single database table and <tab> is either a table or another join expression. The database tables can be specified statically or dynamically as described above. You may also use aliases. You can enclose each join expression in parentheses. The INNER addition is optional.
    A join expression links each line of <tab> with the lines in <dbtab> that meet the condition <cond>. This means that there is always one or more lines from the right-hand table that is linked to each line from the left-hand table by the join. If <dbtab> does not contain any lines that meet the condition <cond>, the line from <tab> is not included in the selection.
    The syntax of the <cond> condition is like that of the WHERE clause, although individual comparisons can only be linked using AND. Furthermore, each comparison must contain a column from the right-hand table <dbtab>. It does not matter on which side of the comparison it occurs. For the column names in the comparison, you can use the same names that occur in the SELECT clause, to differentiate columns from different database tables that have the same names.
    The comparisons in the condition <cond> can appear in the WHERE clause instead of the ON clause, since both clauses are applied equally to the temporary table containing all of the lines resulting from the join. However, each join must contain at least one comparison in the condition <cond>.
    Example for INNER JOINS
    REPORT demo_select_inner_join.
    DATA: BEGIN OF wa,
    carrid TYPE spfli-carrid,
    connid TYPE spfli-connid,
    fldate TYPE sflight-fldate,
    bookid TYPE sbook-bookid,
    END OF wa,
    itab LIKE SORTED TABLE OF wa
    WITH UNIQUE KEY carrid connid fldate bookid.
    SELECT pcarrid pconnid ffldate bbookid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ( ( spfli AS p
    INNER JOIN sflight AS f ON pcarrid = fcarrid AND
    pconnid = fconnid )
    INNER JOIN sbook AS b ON bcarrid = fcarrid AND
    bconnid = fconnid AND
    bfldate = ffldate )
    WHERE p~cityfrom = 'FRANKFURT' AND
    p~cityto = 'NEW YORK' AND
    fseatsmax > fseatsocc.
    LOOP AT itab INTO wa.
    AT NEW fldate.
    WRITE: / wa-carrid, wa-connid, wa-fldate.
    ENDAT.
    WRITE / wa-bookid.
    ENDLOOP.
    Regards,
    Shiva Kumar(Reward if helpful).

  • Using a view instead of a select statement

    Greetings Abapers
    Ive got a problem. Im writing a program and one of the things i do is i use 2 select statements which are as follows:
    Read all organisational relationships for all business partneru2019s linked to sales codes for all date ranges
      select * from hrp1001 into table lt_hrp1001  " Get all external business partners
         where plvar   eq '01'
          and  sclas   eq 'BP'
          and  relat   eq '008'
          or   relat   eq 'Z40'.
      select * from hrp1001 appending corresponding fields of table lt_hrp1001       "Get all employees
        where plvar eq '01'
        and   sclas eq 'BP'
        and   relat eq '291'
        and   otype eq 'S'.
    I have however been told that i can use the database view HRVPADIC since the hrp1001 table is giving me duplicats which i dont want. Can anyone tell me how to use this view i.e. syntax and logic. I would really appreciate it.

    Well, I don't see how using the view would be better. Why don't you just get all of the data at once:
    SELECT * FROM hrp1001 INTO TABLE lt_hrp1001
      WHERE     plvar EQ '01'
        AND     sclas EQ 'BP'
        AND ( ( relat EQ '008'
         OR     relat EQ 'Z40' )
         OR     relat EQ '291'
        AND     otype EQ 'S' ).
    (I didn't test this so check it.)
    Rob

  • Performance problem(ANEA/ANEP table) in Select statement

    Hi
    I am using below select statement to fetch data.
    Does the below where statement have performance issue?
    can you Pls suggest.
    1)In select of ANEP table, i am not using all the Key field in where condition. will it have performance problem?
    2)does the order of where condition should be same as in table, if any one field order change also will have effect performance
    SELECT bukrs                           
             anln1                          
             anln2                          
             afabe                          
             gjahr                        
             peraf                         
             lnran                         
             bzdat                          
             bwasl                        
             belnr                         
             buzei                         
             anbtr                       
             lnsan                         
        FROM anep
        INTO TABLE o_anep
        FOR ALL ENTRIES IN i_anla
       WHERE bukrs = i_anla-bukrs          
         AND anln1 = i_anla-anln1          
         AND anln2 = i_anla-anln2          
         AND afabe IN s_afabe              
         AND bzdat =< p_date                
         AND bwasl IN s_bwasl.              
      SELECT bukrs      
             anln1      
             anln2      
             gjahr      
             lnran       
             afabe      
             aufwv       
             nafal   
             safal       
             aafal      
             erlbt    
             aufwl      
             nafav    
             aafav     
             invzv   
             invzl      
        FROM anea
        INTO TABLE o_anea
        FOR ALL ENTRIES IN o_anep
       WHERE bukrs = o_anep-bukrs    
         AND anln1 = o_anep-anln1    
         AND anln2 = o_anep-anln2    
         AND gjahr = o_anep-gjahr    
         AND lnran = o_anep-lnran   
         AND afabe = o_anep-afabe.
    Moderator message: Please Read before Posting in the Performance and Tuning Forum
    Edited by: Thomas Zloch on Aug 9, 2011 9:37 AM

    1. Yes. If you have only a few primary keys in youe WHERE condition that does affect the performance. But some times requirement itself may be in that way. We may not be knowing all the primary keys to given them in WHER conditon. If you know the values, then provide them without fail.
    2. Yes. It's better to always follow the sequence in WHERE condition and even in the fields being fetched.
    One important point is, whenever you use FOR ALL ENTRIES IN, please make sure that the itab IS NOT INITIAL i.e. the itab must have been filled in. So, place the same conditin before both the SELECT queries like:
    IF i_anla[] IS NOT INITIAL.
    SELECT bukrs                           
             anln1                          
             anln2                          
             afabe                          
             gjahr                        
             peraf                         
             lnran                         
             bzdat                          
             bwasl                        
             belnr                         
             buzei                         
             anbtr                       
             lnsan                         
        FROM anep
        INTO TABLE o_anep
        FOR ALL ENTRIES IN i_anla
       WHERE bukrs = i_anla-bukrs          
         AND anln1 = i_anla-anln1          
         AND anln2 = i_anla-anln2          
         AND afabe IN s_afabe              
         AND bzdat =< p_date                
         AND bwasl IN s_bwasl.              
    ENDIF.
    IF o_anep[] IS NOT INITIAL.
      SELECT bukrs      
             anln1      
             anln2      
             gjahr      
             lnran       
             afabe      
             aufwv       
             nafal   
             safal       
             aafal      
             erlbt    
             aufwl      
             nafav    
             aafav     
             invzv   
             invzl      
        FROM anea
        INTO TABLE o_anea
        FOR ALL ENTRIES IN o_anep
       WHERE bukrs = o_anep-bukrs    
         AND anln1 = o_anep-anln1    
         AND anln2 = o_anep-anln2    
         AND gjahr = o_anep-gjahr    
         AND lnran = o_anep-lnran   
         AND afabe = o_anep-afabe.
    ENDIF.

  • Dynamic internal table from SELECT - statement ?

    Hi, is it possible to define an internal table just after a select statement is executed so that this internal table holds all the data that come back from the statement ?
    thanks in advance,

    Check the link -
    Re: Create Dynamic internal table
    Regards,
    Amit

  • How an INDEX of a Table got selected when a SELECT query hits the Database

    Hi All,
    How an Index got selected when a SELECT query hits the Database Table.
    My SELECT query is as ahown below.
        SELECT ebeln ebelp matnr FROM ekpo
                       APPENDING TABLE i_ebeln
                       FOR ALL ENTRIES IN i_mara_01
                       WHERE werks = p_werks      AND
                             matnr = i_mara_01-matnr AND
                             bstyp EQ 'F'         AND
                             loekz IN (' ' , 'S') AND
                             elikz = ' '          AND
                             ebeln IN s_ebeln     AND
                             pstyp IN ('0' , '3') AND
                             knttp = ' '          AND
                             ko_prctr IN r_prctr  AND
                             retpo = ''.
    The fields in the INDEX of the Table EKPO should be in the same sequence as in the WHERE clasuse?
    Regards,
    Viji

    Hi,
    You minimize the size of the result set by using the WHERE and HAVING clauses. To increase the efficiency of these clauses, you should formulate them to fit with the database table indexes.
    Database Indexes
    Indexes speed up data selection from the database. They consist of selected fields of a table, of which a copy is then made in sorted order. If you specify the index fields correctly in a condition in the WHERE or HAVING clause, the system only searches part of the index (index range scan).
    The primary index is always created automatically in the R/3 System. It consists of the primary key fields of the database table. This means that for each combination of fields in the index, there is a maximum of one line in the table. This kind of index is also known as UNIQUE. If you cannot use the primary index to determine the result set because, for example, none of the primary index fields occur in the WHERE or HAVING clause, the system searches through the entire table (full table scan). For this case, you can create secondary indexes, which can restrict the number of table entries searched to form the result set.
    reference : help.sap.com
    thanx.

  • Dynamic table in select statement

    Hi All,
    I have a requirement where i have a select option in which the input is table name , i need to use this input in my select statement and fetch entries in the table where bname is eq to ' ' or uname eq ' '.
    the challenge that i am facing is not all the tables have both the fields (uname and Bname).
    Request your help on how to code the above requirements.
    Thanks,
    Bhanu.

    Bhanu,
    I don't know what exactly is your requirement but I don't see any reason why a business user enters a table name as input in the selection screen.
    MANDT NAME                           TYPE NUMB SIGN OPTI   LOW    HIGH
    223   ZPROGNAME                       P    0001     I        EQ    XXX      ZTABLENAME
    You can maintain an entry like the above in TVARVC table and get the table name using a select query on TVARVC and then use the table as below
    SELECT SINGLE * FROM (l_tabname)
               into g_wa_tab2
               WHERE quosrc   EQ g_wa_tab1-quosrc  AND
                     quotyp   EQ g_wa_tab1-quotyp  AND
                     quotno   EQ g_wa_tab1-quotno  AND
                     quotdate EQ g_wa_tab1-quotdate.
    Funda is using TAVRVC to maintain the table names and determine the table based on a condition and the using the table in the select query.
    Thanks,
    K.Kiran.

  • Calling a user defined function in a select statement

    PLS-00231: function 'F_GET_PROJECT_ID' may not be used in SQL
    I am caling a user defined function 'F_GET_PROJECT_ID' in a select statement and getting the above error .
    Can any one help me to resolve it.
    I can not replace the function with a local variable nor can I assign the output of the function to a variable and use the variable in the sql stmt. cos, the in put parameters of the function comes from the same select statement.
    Please help
    Thanks in advance

    Can you provide your function code? Using a function like that is possible from the below example. I suspect something in your function code.
    SQL> create or replace function sample_func(p_sal number)
      2  return number
      3  is
      4  v_sal number;
      5  begin
      6     v_sal := p_sal+100;
      7     return v_sal;
      8  end;
      9  /
    Function created.
    SQL>
    SQL> select empno, ename, sal, sample_func(sal)
      2  from emp
      3  /
         EMPNO ENAME             SAL SAMPLE_FUNC(SAL)
          7839 KING             5000             5100
          7698 BLAKE            2850             2950
          7782 CLARK            2450             2550
          7566 JONES            2975             3075
          7654 MARTIN           1250             1350
          7499 ALLEN            1600             1700
          7844 TURNER           1500             1600
          7900 JAMES             950             1050
          7521 WARD             1250             1350
          7902 FORD             3000             3100
          7369 SMITH             800              900
          7788 SCOTT            3000             3100
          7876 ADAMS            1100             1200
          7934 MILLER           1300             1400
    14 rows selected.
    SQL>And yeah... your formatted code is this.
    cursor c1 is
       SELECT t.upi_nbr upi_nbr,
              f_get_project_id(l.pay_type_code,
                               l.charge_type_nme,
                               l.charge_code) project_id,
              LAST_DAY(TO_DATE(SUBSTR(t.Year_Month, 5, 2)||'/'||'01'||'/'||SUBSTR(t.Year_Month,1,4),
                               'MM/DD/YYYY'))reporting_period_end_date,
              SUM (c.hours_worked_qty) reported_hrs
       from trs.trs_timesheet@oraprod5 T,
            trs.trs_line@oraprod5 L,
            trs.trs_cell@oraprod5 C
    where T.upi_nbr=L.upi_nbr
    and T.year_month=L.year_month
    and L.row_nbr=C.row_nbr
    and L.upi_nbr=C.upi_nbr
    and L.year_month = C.year_month
    and L.invalid_activity_ind = 'V'
    and rtrim(L.charge_code) is not null
    AND L.Pay_Type_Code<>'REQ'
    and C.Hours_Worked_Qty > 0
    GROUP BY t.upi_nbr,
             t.year_month,
             t.oui_nbr,
             l.charge_code,
             l.activity_detail_code,
             l.charge_type_nme,
             l.pay_type_code;Cheers
    Sarma.

  • I can't use my field-symbols in my select statement...

    Hello experts,
    I am have been having problems with my report for the past days. My problem is,
    I declared a dynamic field symbol so that I can use it in my select statement
    instead of the standard internal table. But it is giving me a dump. Below is my code:
    REPORT zfr_forex_rev_acctg
           NO STANDARD PAGE HEADING
           LINE-COUNT 0
           LINE-SIZE 100.
    Data dictionary tables                       *
    TABLES: bsis,
            bsas,
            tcurr,
            t001.
    FIELD-SYMBOLS: <fs_dyntable> TYPE STANDARD TABLE,
                   <fs_dynline>  TYPE ANY,
                   <fs_fld>      TYPE ANY,
                   <fs_data>     TYPE REF TO data,
                   <fs_1>        TYPE ANY,
                   <fs_2>        TYPE ANY TABLE.
    CLASS lcl_main DEFINITION.
      PUBLIC SECTION.
        METHODS: build_table,
                 get_data,
                 combine_data,
                 display_header,
                 display_results.
      PRIVATE SECTION.
        TYPES: BEGIN OF t_bsis_bsas,
                zuonr  TYPE bsis-zuonr,
                gjahr  TYPE bsis-gjahr,
                belnr  TYPE bsis-belnr,
                bldat  TYPE bsis-bldat,
                waers  TYPE bsis-waers,
                blart  TYPE bsis-blart,
                dmbtr  TYPE bsis-dmbtr,
                wrbtr  TYPE bsis-wrbtr,
                hkont  TYPE bsis-hkont,
                amount TYPE bsis-wrbtr,
               END OF t_bsis_bsas.
        DATA: it_bsis_bsas TYPE STANDARD TABLE OF t_bsis_bsas,
              v_counter TYPE i.
      data declarations for internal table
        DATA: lt TYPE lvc_t_fcat,
              ls TYPE lvc_s_fcat,
              fldname(50) TYPE c,
              it_ddfields TYPE TABLE OF ddfield,
              wa_ddfields LIKE LINE OF it_ddfields.
    ENDCLASS.
    CLASS lcl_main IMPLEMENTATION.
    METHOD build_table
      METHOD build_table.
        DATA: lv_from    TYPE bsis-gjahr,
              lv_asof    TYPE bsis-gjahr,
              lv_check   TYPE i,
              lt_data TYPE REF TO data,
              lt      TYPE lvc_t_fcat.
        FIELD-SYMBOLS: <fs_year> TYPE gjahr.
        ASSIGN lv_asof TO <fs_year>.
        lv_from = p_year.
        lv_asof = p_asof+0(4).
        v_counter = lv_asof - lv_from.
        wa_ddfields-fieldname = 'ZUONR'.
        wa_ddfields-position  = '0001'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000018'.
        wa_ddfields-decimals  = '00000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'GJAHR'.
        wa_ddfields-position  = '0002'.
        wa_ddfields-datatype  = 'NUMC'.
        wa_ddfields-leng      =  '000004'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'BELNR'.
        wa_ddfields-position  = '0003'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000010'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'BLDAT'.
        wa_ddfields-position  = '0004'.
        wa_ddfields-datatype  = 'DATS'.
        wa_ddfields-leng      =  '000008'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'WAERS'.
        wa_ddfields-position  = '0005'.
        wa_ddfields-datatype  = 'CUKY'.
        wa_ddfields-leng      =  '000005'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'BLART'.
        wa_ddfields-position  = '0006'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000002'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'DMBTR'.
        wa_ddfields-position  = '0007'.
        wa_ddfields-datatype  = 'CURR'.
        wa_ddfields-leng      =  '000013'.
        wa_ddfields-decimals  = '000002'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'WRBTR'.
        wa_ddfields-position  = '0008'.
        wa_ddfields-datatype  = 'CURR'.
        wa_ddfields-leng      =  '000013'.
        wa_ddfields-decimals  = '000002'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'HKONT'.
        wa_ddfields-position  = '0009'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000010'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
        wa_ddfields-fieldname = 'AMOUNT'.
        wa_ddfields-position  = '0010'.
        wa_ddfields-datatype  = 'CURR'.
        wa_ddfields-leng      =  '000010'.
        wa_ddfields-decimals  = '000002'.
        APPEND wa_ddfields TO it_ddfields.
        DATA: lv_position TYPE i.
        lv_position = 0011.
        WHILE lv_check < v_counter.
         <fs_year> = lv_asof.
          wa_ddfields-fieldname = <fs_year>.
          wa_ddfields-position  = lv_position.
          wa_ddfields-datatype  = 'NUMC'.
          wa_ddfields-leng      =  '000004'.
          wa_ddfields-decimals  = '000000'.
          APPEND wa_ddfields TO it_ddfields.
          lv_asof = lv_asof - 1.
          ADD 1 TO: lv_check, lv_position.
        ENDWHILE.
        CLEAR lv_position.
        lv_position = 1.
        LOOP AT it_ddfields INTO wa_ddfields.
          ls-col_pos   = lv_position.
          ls-row_pos   = lv_position.
          ls-fieldname = wa_ddfields-fieldname.
          APPEND ls TO lt.
          ADD 1 TO lv_position.
        ENDLOOP.
        CLEAR lv_position.
        ASSIGN lt_data TO <fs_data>.
        CALL METHOD cl_alv_table_create=>create_dynamic_table
         EXPORTING
          it_fieldcatalog = lt
         IMPORTING
          ep_table = <fs_data>
         EXCEPTIONS
          generate_subpool_dir_full = 1
         OTHERS = 2.
        IF sy-subrc <> 0.
        ENDIF.
        ASSIGN <fs_data>->* TO <fs_1>.
        ASSIGN <fs_1> TO <fs_2>.
        ASSIGN <fs_1> TO <fs_dyntable>.
      ENDMETHOD.
    METHOD get_data
      METHOD get_data.
    *get records from BSIS
        SELECT zuonr gjahr belnr bldat waers blart dmbtr wrbtr hkont
        FROM bsis
        INTO  CORRESPONDING FIELDS OF TABLE <fs_dyntable>
        WHERE bukrs = p_bukrs
          AND hkont IN s_hkont
          AND budat <= p_asof.
    *get records from BSAS
        SELECT zuonr gjahr belnr bldat waers blart dmbtr wrbtr hkont
        FROM bsas
        APPENDING CORRESPONDING FIELDS OF TABLE <fs_dyntable>
        WHERE bukrs = p_bukrs
          AND hkont IN s_hkont
          AND budat <= p_asof
          AND augdt > p_asof.
      ENDMETHOD.
    START-OF-SELECTION.
      DATA: main TYPE REF TO lcl_main.
      CREATE OBJECT main.
      CALL METHOD main->build_table.
      CALL METHOD main->get_data.
    Need help on this problem. Thanks a lot guys and take care!

    Hi guys,
    I found out the problem  and Andreas is right. Now, I am having problems when including DMBTR, WRBTR in my select statement. All the others are ok. Here is my modified code. Please suggest what I need to add/modify. Thanks a lot!
    CLASS lcl_main IMPLEMENTATION.
    METHOD build_table
      METHOD build_table.
        DATA: lv_from    TYPE bsis-gjahr,
              lv_asof    TYPE bsis-gjahr,
              lv_check   TYPE i,
              lt_data TYPE REF TO data,
              lt      TYPE lvc_t_fcat.
        FIELD-SYMBOLS: <fs_year> TYPE gjahr.
        ASSIGN lv_asof TO <fs_year>.
        lv_from = p_year.
        lv_asof = p_asof+0(4).
        v_counter = lv_asof - lv_from.
      ZUONR
        wa_ddfields-fieldname = 'ZUONR'.
        wa_ddfields-position  = '0001'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000018'.
        wa_ddfields-decimals  = '00000'.
        APPEND wa_ddfields TO it_ddfields.
      GJAHR
        wa_ddfields-fieldname = 'GJAHR'.
        wa_ddfields-position  = '0002'.
        wa_ddfields-datatype  = 'NUMC'.
        wa_ddfields-leng      =  '000004'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
      BELNR
        wa_ddfields-fieldname = 'BELNR'.
        wa_ddfields-position  = '0003'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000010'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
      BLDAT
        wa_ddfields-fieldname = 'BLDAT'.
        wa_ddfields-position  = '0004'.
        wa_ddfields-datatype  = 'DATS'.
        wa_ddfields-leng      =  '00008'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
      WAERS
        wa_ddfields-fieldname = 'WAERS'.
        wa_ddfields-position  = '0005'.
        wa_ddfields-datatype  = 'CUKY'.
        wa_ddfields-leng      =  '000005'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
      BLART
        wa_ddfields-fieldname = 'BLART'.
        wa_ddfields-position  = '0006'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000002'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
      DMBTR
        wa_ddfields-fieldname = 'DMBTR'.
        wa_ddfields-position  = '0007'.
        wa_ddfields-datatype  = 'CURR'.
        wa_ddfields-leng      =  '000013'.
        wa_ddfields-decimals  = '000002'.
        APPEND wa_ddfields TO it_ddfields.
      WRBTR
        wa_ddfields-fieldname = 'WRBTR'.
        wa_ddfields-position  = '0008'.
        wa_ddfields-datatype  = 'CURR'.
        wa_ddfields-leng      =  '000013'.
        wa_ddfields-decimals  = '000002'.
        APPEND wa_ddfields TO it_ddfields.
      HKONT
        wa_ddfields-fieldname = 'HKONT'.
        wa_ddfields-position  = '0009'.
        wa_ddfields-datatype  = 'CHAR'.
        wa_ddfields-leng      =  '000010'.
        wa_ddfields-decimals  = '000000'.
        APPEND wa_ddfields TO it_ddfields.
      AMOUNT
        wa_ddfields-fieldname = 'AMOUNT'.
        wa_ddfields-position  = '0010'.
        wa_ddfields-datatype  = 'CURR'.
        wa_ddfields-leng      =  '000010'.
        wa_ddfields-decimals  = '000002'.
        APPEND wa_ddfields TO it_ddfields.
        DATA: lv_position TYPE i.
        lv_position = 0011.
      Additional fields for the years
        WHILE lv_check < v_counter.
          wa_ddfields-fieldname = <fs_year>.
          wa_ddfields-position  = lv_position.
          wa_ddfields-datatype  = 'NUMC'.
          wa_ddfields-leng      =  '000004'.
          wa_ddfields-decimals  = '000000'.
          APPEND wa_ddfields TO it_ddfields.
          lv_asof = lv_asof - 1.
          ADD 1 TO: lv_check, lv_position.
        ENDWHILE.
        CLEAR lv_position.
        lv_position = 1.
        LOOP AT it_ddfields INTO wa_ddfields.
          CASE wa_ddfields-fieldname.
            WHEN 'BLDAT'.
              ls-col_pos   = lv_position.
              ls-row_pos   = lv_position.
              ls-fieldname = wa_ddfields-fieldname.
              ls-inttype   = 'D'.
              ls-ref_table = 'BSIS'.
              ls-ref_field = 'BLDAT'.
            WHEN 'WAERS'.
              ls-col_pos   = lv_position.
              ls-row_pos   = lv_position.
              ls-fieldname = wa_ddfields-fieldname.
              ls-ref_table = 'BSIS'.
              ls-ref_field = 'WAERS'.
            when 'DMBTR'.
              ls-col_pos   = lv_position.
              ls-row_pos   = lv_position.
              ls-fieldname = wa_ddfields-fieldname.
              ls-ref_table = 'BSIS'.
              ls-ref_field = 'DMBTR'.
            when 'WRBTR'.
              ls-col_pos   = lv_position.
              ls-row_pos   = lv_position.
              ls-fieldname = wa_ddfields-fieldname.
              ls-ref_table = 'BSIS'.
              ls-ref_field = 'WRBTR'.
            WHEN OTHERS.
              ls-col_pos   = lv_position.
              ls-row_pos   = lv_position.
              ls-fieldname = wa_ddfields-fieldname.
          ENDCASE.
          APPEND ls TO lt.
          ADD 1 TO lv_position.
        ENDLOOP.
        CLEAR lv_position.
        ASSIGN lt_data TO <fs_data>.
        CALL METHOD cl_alv_table_create=>create_dynamic_table
         EXPORTING
          it_fieldcatalog = lt
         IMPORTING
          ep_table = <fs_data>
         EXCEPTIONS
          generate_subpool_dir_full = 1
         OTHERS = 2.
        IF sy-subrc <> 0.
        ENDIF.
        ASSIGN <fs_data>->* TO <fs_1>.
        ASSIGN <fs_1> TO <fs_2>.
        ASSIGN <fs_1> TO <fs_dyntable>.
      ENDMETHOD.

  • Indexed views using indexes on base table

    Hi all,
    CREATE VIEW Sales.vOrders
    WITH SCHEMABINDING
    AS
    SELECT SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Revenue,
    OrderDate, ProductID, COUNT_BIG(*) AS COUNT
    FROM Sales.SalesOrderDetail AS od, Sales.SalesOrderHeader AS o
    WHERE od.SalesOrderID = o.SalesOrderID
    GROUP BY OrderDate, ProductID;
    GO
    --Create an index on the view.
    CREATE UNIQUE CLUSTERED INDEX IDX_V1
    ON Sales.vOrders (OrderDate, ProductID);
    GO
    --This query can use the indexed view even though the view is
    --not specified in the FROM clause.
    SELECT SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Rev,
    OrderDate, ProductID
    FROM Sales.SalesOrderDetail AS od
    JOIN Sales.SalesOrderHeader AS o ON od.SalesOrderID=o.SalesOrderID
    AND ProductID BETWEEN 700 and 800
    AND OrderDate >= CONVERT(datetime,'05/01/2002',101)
    GROUP BY OrderDate, ProductID
    ORDER BY Rev DESC;
    In the above code block, Sales.SalesOrderDetail and Sales.SalesOrderHeader are base tables.
    Say suppose there are some indexes on some of the columns of these base tables. Are these indexes used when we write a query in which indexed view is mentioned
    in the from clause?
    Thanks, Srikar

    SO far as its a indexed view it wont use the indexes on base tables when you use it in a query as indexed view is persisted and exists as a physical object. SO it doent require definition to be substituted and data to be retrieved from the base objects.
    The indexes will come handy while populating the indexed view.
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

Maybe you are looking for

  • False Advertising: Selling Used Tablet as New and Personal Information to the Public

    So last week I purchased an LG G Pad 7.0 for my wife. It was on sale for $126.99. I purchased this tablet as I bought one for myself months back and have been very happy with it. This past Monday it was delivered. My wife called me at work and told m

  • Maximum hard drive capacity f or hp dv8

    I have a HP DV8 with 2 320GB hard drives. I would like to put 2 750 GB WD Black Scorpio drives in the PC. Is this possible? What is the maximum capacity for this laptop? Thanks.

  • Change Form Name

    Dear All, Is it possible to change the form name in any module of SAP B One. I want to change form name of A/R down payment request is it possible to do this in main menu of module. If yes please let me know the procedure. Thanks swapnil

  • Wrong user on updating certain software

    Hello. I'm on my work iMac, and when I try to update some of the apps (Numbers, Keynote, Pages and iMovie) it asks for the previous users password. We did make a user for me, and deleted the old user, as well as I'm signed into the App Store, not him

  • "Length is too big" IOException when using OpenSSL key/certs

    Using WLS 5.1, SP6, Solaris Hello one and all: I am trying to test WLS with SSL. I am using the OpenSSL package to act as my own CA and generate and sign my own server certs. I don't have any problem with this part. However, when I try to use my priv