Select record from database table

how to select a record from data-base table for current month in module-pool, i.e if current month is august then records selected should between 1/08/10 to 31/08/10.
And, i also want to select records for last month i.e if current month is august then records selected should be between 1/07/10 to 31/07/10.
And one thing more, it should work for every scenario. Like for months are changing then it should work according to that.
Example: If month changes to October then current month ll be October and last month ll become September.
Plz help me out.
Moderator message: you chose a better forum this time, but the question is still too basic to be asked here, please (re)search yourself first, these forums are not catering to beginners.
locked by: Thomas Zloch on Aug 8, 2010 9:41 PM

Hi Nishant,
You can use the option <b>ORDER BY </b> in your SELECT clause.
This is the SAP Documentation for <b>ORDER BY </b> option.
<i>Variant 1</i>
<b>...ORDER BY PRIMARY KEY</b>
<b>Effect</b>
Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
<i>Notes</i>
Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
<i>Variant 2</i>
<b>ORDER BY f1 ... fn</b>
<i>Effect</i>
Sorts the selected records in ascending order by the specified column references f1 ... fn. If a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.
By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.
The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
Consider this code this will select the latest 10 records form DB table(Note can use <b>Variant 2</b> for DB tables also.
REPORT zarun_1.
DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.
START-OF-SELECTION.
  SELECT * FROM mara
            INTO TABLE it_mara
            <b>UP TO 10 ROWS                "No of rows you need(give 1 here)
           ORDER BY matnr DESCENDING.    "Specify the Key Fields here.</b>
  LOOP AT it_mara.
    WRITE : / it_mara-matnr,
              it_mara-mtart.
  ENDLOOP.
Regards,
Arun Sambargi.
Message was edited by: Arun Sambargi

Similar Messages

  • Fetch last record from database table

    hi,
    how to fetch last record from database table.
    plz reply earliest.
    Regards,
    Jyotsna
    Moderator message - Please search before asking - post locked
    Edited by: Rob Burbank on Dec 11, 2009 9:44 AM

    abhi,
    just imagine the table to be BSEG or FAGLFLEXA... then what would be performance of the code ?
    any ways,
    jyotsna, first check if you have a pattern to follow like if the primary key field have a increasing number range or it would be great if you find a date field which stores the inserted date or some thing..
    you can select max or that field or order by descending using select single.
    or get all data.. sort in descending order.(again you need some criteria like date).
    read the first entry. using read itab index 1

  • Retrive last inserted  record  from database table

    Hi,
    some body inserting a record into table 'A' through some procedure/java program.i want to retrive the last inserted record from database table.records are not stored in order.Can any body help me.

    In general, unless you are storing a timestamp as part of the row or you have some sort of auditing in place, Oracle has no idea what the "first" or "last" record in a table is. Nor does it track when a row was inserted.
    - If you have the archived logs from the point in time when the row was inserted, you could use LogMiner to find the timestamp
    - If the insert happened recently (i.e. within hours), you may be able to use flashback query to get the value
    - If you're on 10g or later and the table was built with ROWDEPENDENCIES and the insert happened in the last few days and you can deal with a granularity of a few seconds and you don't need 100% accuracy, you could get the ORA_ROWSCN of the row and convert that to a timestamp.
    If this is something you contemplate needing, you need to store the data in the row or set up some sort of auditing.
    Justin

  • I want to delete the transactional record from database table

    Hai,
    I want to delete the transactional data from database table with out using the dialog programming is it there any trancation for this.
    for master data we have the transaction code for delete the records. The t.code is 'OBR2'.
    Plz help in that cse.
    Thanks and regards,
    P.Naganjana Reddy

    Hai,
    Plz help me urgent.
    I am asking i want to delete transactional data from database table without using the dialog programming.
    Thanks and Regards,
    P.Naganjana

  • Latest record from database table

    Hi all,
    I am using select statement on a database table. The table contains records, and each one has an effective date.
    effective dt    record    Rate
    01/01/1800        R1         40
    <b>01/01/2007        R1         30</b>
    01/01/1800        R2         40
    I want to select rates corresponding to R1 and R2. For R2, the rate is 40, but i need the rate 30 and not 40 for R1, because rate 30 has the most recent effective date. Please tell me if i can do this in the select query itself...
    Please help me...
    Thanks
    Pranati.

    Hi,
    Try this..
    Ranges: r_bukrs for bkpf-bukrs.
    LOOP AT l_t_records.
    Build the range.
      r_bukrs-sign  = 'I'.
      r_bukrs-option = 'EQ'.
      r_bukrs-low     = l_t_records.
      append r_bukrs.
    ENDLOOP.
    SORT r_bukrs by low.
    DELETE adjacent duplicates from r_bukrs comparing low.
    SELECT max( zeff_dt ) bukrs zbrdn kostl hkont INTO TABLE l_t_brdncalc
    FROM zfpbrdn
    WHERE bukrs IN r_bukrs
    GROUP BY zbrdn.   " I am not sure of the record field..But you have to give
                                  " GROUP BY clause..give the correct field..
    Hope this helps..
    Thanks,
    Naren

  • Selecting Records from one table where records don't exist in a linked table

    Post Author: RMC
    CA Forum: Crystal Reports
    Tables:
    Stock (Main Table)
    Transactions (Left Outer Join)
    Criteria:
    Select all items with no transactions between a selected date range.
    The items will exist in Main table but I want to select all items that haven't been in a transaction between a date range so I can identify no movement with stock items just sitting in the database doing nothing.
    Any suggestions??

    Post Author: yangster
    CA Forum: Crystal Reports
    Sorry for the delay.  Dam meetings all day yesterday.You are essentially looking for something that doesn't exist in the db so you need to look at it from a different approach.You definitely don't want to use a full outer join.  From what i can tell from your description above you are looking for stocks that are inactive/idle.So you need not look at data that doesn't exist but the last activity date.So you'd want something along the lines ofselect s.stock_name, s.stock_blah, max(t.transaction_date) Transaction_datefrom stocks sjoin transactions t on s.itemid = t.itemidwhere whatever restrictions you need to addgroup by s.stock_name, s.stock_blahthis will produce one record for each stock you have in the stock table showing you only the last date of activityput that in a command in crystalin the selection expert in crystal reports put in a simple criteriatransaction_date <= some datethis will only pull in records based on your date selection so if you say give me all records from dec 31, 2006  you will know all the records being pulled in are stocks that have been idle since dec 31, 2006 or earlierlet me know if this doesn't work

  • Selecting records from DB table with out using internal tables

    hi,
    i need to retrieve values from a database table based on few fields and date as well. however, i need to check whether the date is less or equal to the current date and along with that i should get the appropriate record. how can i do that with out using internal table.
    field1-----date---
    11111----
    20070219
    11111--20070214 <---
    11111----
    20070205
    in the above scenario i should get the second record
    Regards,
    Kranthi.

    Try:
    REPORT ztest MESSAGE-ID 00.
    TABLES bkpf.
    SELECT * FROM bkpf
      UP TO 1 ROWS
      WHERE budat <= sy-datum
      ORDER BY budat DESCENDING.
    ENDSELECT.
    Rob

  • Deletion of  a particular record  from database table

    hai friends,
         i want to <b>delete</b> (or)   <b>insert</b> particular record from  predefined sap table... that is with out module pool programming...  can i delete or insert a record to or from  the table..
         thanks in advance
    @jay

    hi,
    ucan do it without module pool programming in your normal report program.
    To insert a line into an index table, use the statement:
    INSERT <line> INTO <itab> [INDEX <idx>].
    <line> is either a work area that is convertible to the line type, or the expression INITIAL LINE. If you use <wa>, the system adds a new line to the internal table <itab> and fills it with the contents of the work area. INITIAL LINE inserts a blank line containing the correct initial value for each field of the structure.
    To select the lines that you want to delete using a condition, use the following:
    DELETE FROM <target> WHERE <cond> .
    All of the lines in the database table that satisfy the conditions in the WHERE clause are deleted. The FROM expression must occur between the keyword and the database table.
    You should take particular care when programming the WHERE clause to ensure that you do not delete the wrong lines. For example, if you specify an empty internal table in a dynamic WHERE clause, all of the lines in the table are deleted.
    If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4. SY-DBCNT contains the number of lines deleted.
    regards,
    keerthi

  • Select double * from database table?

    Hi,
    See the entries in the following database table.
    TABLE1
    F1 -- F2 -- F3 -- F4 -- F5 etc..,
    AB   12    de    effd    32e
    CD   34    fr      wref   45fr
    EF   34    rfr      wfrf    wfwrf
    Here I need to get the two values of F1 field. Those are AB and CD into two separate variables.
    How can get these two values in to two variables with out DECLARING INTERNAL TABLE and with ONE SELECT QUERY?
    --Naveen Inuganti.

    Hi ,
    Check the code , it will resolve the issue --
    DATA : w_variable2 TYPE scarr-carrid,
           w_variable1 TYPE scarr-carrid .
    SELECT carrid UP TO 2 ROWS FROM scarr
    INTO w_variable2 .
      MOVE w_variable2 TO w_variable1.
    ENDSELECT.
    WRITE : w_variable1, w_variable2.
    From  your reference I created single silect query without internel table - -
    DATA : w_variable2 TYPE dbtable-f1,
           w_variable1 TYPE dbtable-f1 .
    SELECT F1 UP TO 2 ROWS FROM dbtable
    INTO w_variable2 .
      MOVE w_variable2 TO w_variable1.
    ENDSELECT.
    WRITE : w_variable1, w_variable2.
    Regards
    Pinaki

  • Can i select Records from two tables into an itab.

    Hi,
        Suppose that i have two tables spfli and sflight.
        Now i want the records from both the tables into a single internal table.
        If so please let me know how this can be done.
        itab should consist of all the columns in sflight and spfli.
    Regards,
    Sai

    Sai,
    Yes u can do it using JOINs.
    Use this link for ref
    http://www.sap-img.com/abap/inner-joins.htm
    INNER JOIN results are an intersection of the tables being joined where in only if both the tables havethe data the result is pused onto the result set.
    WHERE as in LEFT OUTR JOIN you can push the data of the LEF T table on the resultset even when the join condition is not met.
    The use is that you wantto have all the data that is there in left table and also the right table if the join condition is a success then right table fileds will have data else they are initial.
    OUTJOIN's are used in MAINTENANNCE VIEWS, HELP VIEWS .
    INNER JOINS are used DATABSE VIEWS.
    Inner Join:-
    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.
    Left Outer Join
    SELECT scarrid scarrname p~connid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM scarr AS s
    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND
    p~cityfrom = 'FRANKFURT'.
    Reward if helpful,
    Karthik

  • Select data from database tables with high performance

    hi all,
    how to select data from different database tables with high performance.
    im using for all entries instead of inner joins, even though burden on data base tables is going very high ( 90 % in se30)
    hw to increase the performance.
    kindly, reply.
    thnks

    Also Check you are not using open sql much like distict order by group by , use abap techniques on internal table to acive the same.
    also Dont use select endselect.
    if possible use up to n rows claus....
    taht will limit the data base hits.
    also dont run select in siode any loops.
    i guess these are some of the trics oyu can use to avoid frequent DATA BASE HITS AND ABVOID THE DATA BASE LAOD.

  • Display records from database table

    My database table has 495K records i want to display them all in se11 tcode and than download into excel ...but because of huge records it timeout.
    Do I need to write a program for this?
    Or is there any other smarter way.
    Regards,
    DNP

    Hi,
    Please follow the steps below:
    1. Go to the selection screen of SE11 and provide your selection criteria (if required).
    2. Click on Settings menu->User parameters->ALV Grid display.
    3. Now click on Program menu->Execute in background->Provide output device name as LP01 or LOCAL or any valid printer parameters->Click continue->Click Immediate button->Click Save.
    4. You can see the background job for this in SM37.
    5. Once the background job is completed, it will appear in gleen color.
    6. Click on the completed job and then click on Spool button.
    7. Once the output is displayed in spool you can now easily download it to excel by clicking on System menu->List->Save->Local File.
    Hope this will help you.
    Regards,
    Venkat

  • SELECTing records from two tables. Set Operators, CASE, DECODE, ...

    Hi all,
    I have two tables:
    CRETE TABLE T1 (T1_COL1 NUMBER, T1_COL2 NUMBER)
    CRETE TABLE T2 (T2_COL1 NUMBER, T2_COL2 NUMBER)
    T1 may or may not have records. T2 always has records. There are two scenarios.
    Scenario 1:
    =======
    SELECT * FROM T1 returns five rows, and SELECT * FROM T2 returns 10 rows.
    Now I need the five rows from T1.
    Scenario 2:
    =======
    SELECT * FROM T1 returns zero rows, and SELECT * FROM T2 returns 10 rows.
    Now I need the 10 rows from T2.
    In other words, if records present in T1, I need them all. If not, I need records from T2.
    There are no common columns (for joins).
    Now need a single query to achive this. I tried set operators, CASE and DECODE. But I'm unable to solve it.
    Please help. Thanks in advance.

    Iniyavan wrote:
    Yes, Justin. I'm sure that this is the way it's modelled. I also find it's tough and odd.Are you sure it's the right way for that data to be modeled? I understand that's the way it is being modeled, but a data model change may be the best option.
    Is there any other way, which is simpler, without using RANK?That's the simplest option I can think of. You could also do something like
    SELECT t1_col1, t1_col2
      FROM (
        SELECT t1_col1, t1_col2, rownum rn
          FROM (
            SELECT t1_col1, t1_col2
              FROM (
                SELECT t1_col1, t1_col2, 1 tbl
                  FROM t1
                UNION ALL
                SELECT t2_col2, t2_col2, 2 tbl
                  FROM t2       
             ORDER BY tbl
    WHERE rn = 1I'm not sure that's any simpler...
    Justin

  • How to pass selected records from one table to another ?

    Hi,
    In my view i have designed a table with certain records.  I need to pass only specific records of this table to another table which been designed in another view. Can anybody please give sum idea for this.
    Rgds
    Sudhanshu

    hi,
    Refer the below  code:
    1. I have selected some data from the table.
    2. The selected data is moved to some other internal table.
    3. Internal table is further binded to the node in which data is to be shown.
    data : lo_nd type ref to if_wd_context_node,
      lo_nd1 type ref to if_wd_context_node,
      lt_temp type wdr_context_element_set,
      wa_temp type ref to if_wd_context_element,
      ls_node1 type sflight,
      lt_node1 type STANDARD TABLE OF sflight.
    lo_nd = wd_context->get_child_node('CN_MAIN').  <CN_MAIN is my node>
      CALL METHOD lo_nd->get_selected_elements  <here selected data is moved to lt_temp>
       RECEIVING
           set = lt_temp.
      loop at lt_temp INTO wa_temp.
          CALL METHOD wa_temp->get_static_attributes
          IMPORTING
            static_attributes = ls_node1.       <Selected data in work area.>
        APPEND ls_node1 TO lt_node1.  < Data moved to internal Table>
        CLEAR ls_node1.
      ENDLOOP.
    Finally Internal table is binded to the node required.
      lo_nd1 = wd_context->get_child_node('CN_MAIN2').
      lo_nd1->bind_table( lt_node1 ).
    In your case , map this CN_MAIN2 with the Component Controller and from component controller you can access data in your second view also.
    I hope it helps.
    Thanx.
    Saurav.

  • How i can Insert selected records in database table using check box.

    Hi Friends,
    I have non database block, which displayed multiple records, now I add a Check Box to this block and now I want to insert the records in a database table which I checked. So when I press a button, all the checked records are inserted in the table.
    Please send me the code of this solution.
    Thanks in advance.
    Shahzad

    I have almost the exact scenario but instead of inserting into the DB, I want to find the Checked records and process them in PL/SQL script.
    I currently have a cursor that looks for all the checked records. It's only getting the first record, so I figured I need to loop through the block looking for checked records.
    Also can I save them to a temp or PL/SQL table to process later?

Maybe you are looking for