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

Similar Messages

  • How to fetch last record from the table to display in screen painter?

    Hi Gurus,
                  I have a requirement where i have to fetch the last record value in the table.
    I have a screen where the TEXT name is REQUEST and I/P field name is REQ and SAVE push button.
    In this I/P field automatically 1 should display when i excecute the program and when i save this, the request number should turn to 2 that is dynamically..once agin save then 3 and so on....and this 1, 2, 3 will be storeing in table.
    I have succeeded till generating the values dynamically and saving in the table like 1 is displayed and when i click save it is changeing to 2..but the problem is when i am coming out of program and logon again the request number is again displaying as 1 rather it has to start with 4 because already 3 request are there in the table.
    My code is...
    Table zsave.
    data declarations....
    data req type i.
    req = 0.
    req = zsave-req.
    PROCESS BEFORE OUTPUT
    req = 0.
    select req into table fun_st from zsave.
    req = zsave-req.
    zsave-req = req.
    req = zsave-req + 1.
    PAI
    req = zsave-req + 1.
    zsave-req = req.
    when 'save'.
    wa_funst-req = zsave-req.
    Please can you provide me with the solution....answer will be rewared.
    THKS

    Can you tell me, when updaing the ZSAVE table with the counter value are you modifying the exisiting value or inserting the value as new record?
    From the code in your first post, i see that you are extracting records into internal table but when you are moving to variable REQ, you are moving from table work area which is blank, so everytime when you execute it may be starting as 0.
    Hope below code can help you understand on handling your requirement
    Global Declaration:
    >DATA: l_req TYPE i. 
    PBO:
      SELECT MAX( req ) INTO l_req FROM zsave.
      l_req = l_req + 1.
    PAI:
      DATA: wa TYPE zsave.
      CASE ucomm.
      WHEN 'SAVE'.
        wa-req = l_req.
        INSERT zsave FROM wa.
      ENDCASE.

  • 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

  • Issue with fetching a record from IHPA table with PARNR value as condition

    Hi All,
    I am trying to fetch a particular record from IHPA table where i am giving the OBJNR number, PARVW and PARNR as customer no. , But I am not getting any record  but if i will give OBJNR and PARVW it will generate a record with having same PARNR value.
    Example :
    case 1st :-
    when i am passing following values in IHPA table,
    OBJNR  = IE000000000010070910
    PRAVW = BP                 
    PARNR  = 620000562   
    it is not generating any record  
    case 2nd:-
    But if i will pass  
    OBJNR  = IE000000000010070910
    PRAVW = BP
    it will generate a record?(one row) which will include PARNR as same 620000562 along with other value.
    Q1 -  Why it's not generating in case 1st,
    Q2 - How I will resolve this issue ? as i have to link some table as EQUI -
    IHPA -
    VCKUN
    VCNUM ?
          I have written a code for this to join :
         SELECT A~EQUNR
           B~PARVW
           C~CCNUM
           D~CCNUM
           FROM EQUI AS A JOIN IHPA AS B ON AOBJNR = BOBJNR
           INNER JOIN VCKUN AS C ON BPARNR = CKUNNR
           INNER JOIN VCNUM AS D ON CCCNUM = DCCNUM
           APPENDING CORRESPONDING FIELDS OF TABLE IT_OBSO
           WHERE A~EQUNR IN S_EQN.
                 AND PARVW  = 'BP'.
          But it is not generating any record but when i am commenting the PARVW i am getting the mixed record of PARVW as (BP,SP,SH)
    Let me know if any more clarification required.
    Highly appreciate your help
    Thanks & Regards
    Shaw

    Hi Santosh,
    Thanks for your quick response.
    But I am Sorry, I already checked with putting Zero before PARNR.
    The issue is still as it is.
    Thanks
    Shaw

  • 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

  • How to fetch corresponding records from 2 tables using ADF Toplink & EJB

    Hi,
    I am unable to fetch records from 2 tables, which has a foregin key.Used the custom query in toplink, But the output is the cartition of the first table draged and the first row of the second table.The output required should be in the form of ADF Table. I am able to find the output in JSF data table.
    Two tables:
    Table name:Solutions
    sol_id varchar2(PK)
    sol_name varchar2
    Table name:Solution_details
    sol_id varchar2(FK)
    common_asum_id varchar2(PK)
    common_detail varchar2
    Output Required:
    sol_id,sol_name,common_asum_id,common_detail
    Custom query:
    SELECT * FROM solutions a
    ,solution_details b
    WHERE a.sol_id=b.sol_id

    Hi,
    I am unable to fetch records from 2 tables, which has a foregin key.Used the custom query in toplink, But the output is the cartition of the first table draged and the first row of the second table.The output required should be in the form of ADF Table. I am able to find the output in JSF data table.
    Two tables:
    Table name:Solutions
    sol_id varchar2(PK)
    sol_name varchar2
    Table name:Solution_details
    sol_id varchar2(FK)
    common_asum_id varchar2(PK)
    common_detail varchar2
    Output Required:
    sol_id,sol_name,common_asum_id,common_detail
    Custom query:
    SELECT * FROM solutions a
    ,solution_details b
    WHERE a.sol_id=b.sol_id

  • 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

  • Need help for SQL SELECT query to fetch XML records from Oracle tables having CLOB field

    Hello,
    I have a scenario wherein i need to fetch records from several oracle tables having CLOB fields(which is holding XML) and then merge them logically to form a hierarchy XML. All these tables are related with PK-FK relationship. This XML hierarchy is having 'OP' as top-most root node and ‘DE’ as it’s bottom-most node with One-To-Many relationship. Hence, Each OP can have multiple GM, Each GM can have multiple DM and so on.
    Table structures are mentioned below:
    OP:
    Name                             Null                    Type        
    OP_NBR                    NOT NULL      NUMBER(4)    (Primary Key)
    OP_DESC                                        VARCHAR2(50)
    OP_PAYLOD_XML                           CLOB       
    GM:
    Name                          Null                   Type        
    GM_NBR                  NOT NULL       NUMBER(4)    (Primary Key)
    GM_DESC                                       VARCHAR2(40)
    OP_NBR               NOT NULL          NUMBER(4)    (Foreign Key)
    GM_PAYLOD_XML                          CLOB   
    DM:
    Name                          Null                    Type        
    DM_NBR                  NOT NULL         NUMBER(4)    (Primary Key)
    DM_DESC                                         VARCHAR2(40)
    GM_NBR                  NOT NULL         NUMBER(4)    (Foreign Key)
    DM_PAYLOD_XML                            CLOB       
    DE:
    Name                          Null                    Type        
    DE_NBR                     NOT NULL           NUMBER(4)    (Primary Key)
    DE_DESC                   NOT NULL           VARCHAR2(40)
    DM_NBR                    NOT NULL           NUMBER(4)    (Foreign Key)
    DE_PAYLOD_XML                                CLOB    
    +++++++++++++++++++++++++++++++++++++++++++++++++++++
    SELECT
    j.op_nbr||'||'||j.op_desc||'||'||j.op_paylod_xml AS op_paylod_xml,
    i.gm_nbr||'||'||i.gm_desc||'||'||i.gm_paylod_xml AS gm_paylod_xml,
    h.dm_nbr||'||'||h.dm_desc||'||'||h.dm_paylod_xml AS dm_paylod_xml,
    g.de_nbr||'||'||g.de_desc||'||'||g.de_paylod_xml AS de_paylod_xml,
    FROM
    DE g, DM h, GM i, OP j
    WHERE
    h.dm_nbr = g.dm_nbr(+) and
    i.gm_nbr = h.gm_nbr(+) and
    j.op_nbr = i.op_nbr(+)
    +++++++++++++++++++++++++++++++++++++++++++++++++++++
    I am using above SQL select statement for fetching the XML records and this gives me all related xmls for each entity in a single record(OP, GM, DM. DE). Output of this SQL query is as below:
    Current O/P:
    <resultSet>
         <Record1>
              <OP_PAYLOD_XML1>
              <GM_PAYLOD_XML1>
              <DM_PAYLOD_XML1>
              <DE_PAYLOD_XML1>
         </Record1>
         <Record2>
              <OP_PAYLOD_XML2>
              <GM_PAYLOD_XML2>
              <DM_PAYLOD_XML2>
              <DE_PAYLOD_XML2>
         </Record2>
         <RecordN>
              <OP_PAYLOD_XMLN>
              <GM_PAYLOD_XMLN>
              <DM_PAYLOD_XMLN>
              <DE_PAYLOD_XMLN>
         </RecordN>
    </resultSet>
    Now i want to change my SQL query so that i get following output structure:
    <resultSet>
         <Record>
              <OP_PAYLOD_XML1>
              <GM_PAYLOD_XML1>
              <GM_PAYLOD_XML2> .......
              <GM_PAYLOD_XMLN>
              <DM_PAYLOD_XML1>
              <DM_PAYLOD_XML2> .......
              <DM_PAYLOD_XMLN>
              <DE_PAYLOD_XML1>
              <DE_PAYLOD_XML2> .......
              <DE_PAYLOD_XMLN>
         </Record>
         <Record>
              <OP_PAYLOD_XML2>
              <GM_PAYLOD_XML1'>
              <GM_PAYLOD_XML2'> .......
              <GM_PAYLOD_XMLN'>
              <DM_PAYLOD_XML1'>
              <DM_PAYLOD_XML2'> .......
              <DM_PAYLOD_XMLN'>
              <DE_PAYLOD_XML1'>
              <DE_PAYLOD_XML2'> .......
              <DE_PAYLOD_XMLN'>
         </Record>
    <resultSet>
    Appreciate your help in this regard!

    Hi,
    A few questions :
    How's your first query supposed to give you an XML output like you show ?
    Is there something you're not telling us?
    What's the content of, for example, <OP_PAYLOD_XML1> ?
    I don't think it's a good idea to embed the node level in the tag name, it would make much sense to expose that as an attribute.
    What's the db version BTW?

  • 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

  • How to fetch last record from a flatfile

    I have a flatfile with 5000+ records and last record is the count record , i want to fetch the last record only through sql loader or External table (OS IS Unix)
    Pls any body can give the answer
    Kiran Kumar Thayyuru
    Message was edited by:
    user557500

    you migth want to take a look at the option CONTINUEIF LAST

  • 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

  • To fetch ALTERNATE records from a table. (EVEN NUMBERED)

    suppose i having tABle employee columns empid,empname ,emplocation it having 20 rows and  i want to fetch 10 records which are even how to write query
    jitendra

    it was interview question
    Then you ask back to the interview: what do you mean with even rows? In a relational database there is no inherit number for a row. In fact a table is by definition an unordered set. Of course, we can extract only the even empid:s, but there is no guarantee
    that there is exactly ten of them. Chance could have it that all empids are odd.
    If the interviewer insists you can say that we can always run a query with row_number, but that frankly does not make any sense.
    If nothing else, this sort of response may reveal what sort of interviewer you are talking to. Is is exactly as clueless as the question suggest? Or is he purposely asking you a stupid question to see if you are smart enough to call his bluff?
    I don't know about interviews in general, but if I were interviewing people, I would certainly be impressed by someone who has the guts to object and come with sound objections. That is exactly the kind of people I want to work with.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • 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

  • Deleting records from database table

    Hi Experts,
    In this program i used Table Control.
    Here i want to delete the selected Rows. But Here if i say delete the selected row , it is deleting the last row. Why ?
    Please help me It's Urgent.
    Thanks,
    Basu
    MODULE USER_COMMAND_9000 INPUT.
      DATA: LV_FCODE LIKE SY-UCOMM.
      LV_FCODE = SY-UCOMM.
      CASE LV_FCODE.
        WHEN 'CHANGE'.
          FLG = 'Y'.
        WHEN 'DELETE'.
          FLG = 'Y'.
          LOOP AT I_MAKT.
            DATA NUM TYPE N.
            NUM = SY-TABIX.
            READ TABLE I_MAKT INDEX NUM INTO I_MAKT1-EMPID.
            MOVE I_MAKT1-EMPID TO I_MAKT2-EMPID.
          ENDLOOP.
          UPDATE ZEMPLOYE FROM TABLE I_MAKT.
          DELETE I_MAKT WHERE EMPID = I_MAKT2-EMPID.
          DELETE FROM ZEMPLOYE WHERE EMPID = I_MAKT2-EMPID.
          MESSAGE S001(YTC).
        WHEN 'INSERT'.
          MODIFY ZEMPLOYE FROM TABLE I_MAKT.
          MESSAGE S002(YTC).
        WHEN 'BACK'.
          SET SCREEN 0.
        WHEN 'EXIT' OR 'CANCEL'.
          LEAVE PROGRAM.
      ENDCASE.
    ENDMODULE.                 " USER_COMMAND_9000  INPUT

    Hi,
    See following link:
    http://www.sapmaterial.com/tablecontrol_sap.html
    Reward points if helpful.
    Regards.
    Srikanta Gope.

  • Form is not Pulling records from database table.

    Hi all,
    it seems very simple question for you, but i got stuck here.
    I have created one table and insert some records in it from SQL.
    then i create one form and create a data block and pull all the fields form the table, now when i run this form it will not show the records... why ?
    is anything wrong am i doing ?
    Thanks for any help.
    Abid

    Yes when i press Execute Query it shows the records.
    can i add this on form level, i mean when form will load i can execute this query.
    how will this work.
    on_form_load
    execute_query;
    is that right ?
    Thanks for giving me clue, you soloved my problem.
    Abid

Maybe you are looking for

  • Memory upgrades and battery scratches

    Good afternoon all! I couldn't find where to email Lenovo directly so unfortunately I'm going to have to post here. I'm probably going to catch some flak for this but hey ho, so heres the thing, I bought two brand new Lenovo 3000 N100's 5 years ago,

  • How to draw custom line in crystal report without manually entering value?

    Hi, I have a bubble chart and want to represent custom lines one in x-axis and one in y-axis at the value arrived based upon an internal calculation.There is an option to manually enter value for custom line. But i want to generate custom line at run

  • SQL Loader bug

    2 day ago I meet with problem to load text file to Oracle DB with SQL statment in control file. It's my control file: LOAD DATA INFILE '/tmp/123/22-12.txt' REPLACE INTO TABLE VL_LOG (date_time SYSDATE, SOURCE POSITION(02:16) "TRUNC(SUBSTR(:SOURCE,1,3

  • Can I transfer pictures stored on my pc to my ipad2? Pc has windows7 and did the original sync but have new pictures to add.

    Can I transfer pictures stored on my pc to ipad2?  Pc has windows7 and I did the original sync but have additional pictures now.

  • Need idea about table index

    Hi all, could someone please advise me tool for definition of needed index? In other words, I got query with "table access full", because one of tables doesn't have any indexes, so I should know what index would be the best to create. I don't say tha