Last inserted record

HI,
we have created a table with a field ID.
we have inserted 1000 records in that table,.
I want to retrieve only the last inserted record.
How it's possible?
is it possible with rowid?
e.g:
CREATE TABLE TEST(id number(2));
insert into TEST values(1);
insert into TEST values(2);
insert into TEST values(3);
insert into TEST values(4);
insert into TEST values(5);
In this case i want to retrieve only 5.
How we can achieve this?

There is no concept of order in a heap table. Blocks can be reused so the ROWID cannot ever be used to determine when a row was inserted. The only way to determine the last row that was inserted into a table is to use a timestamp and/or a sequence column. If you need to determine the last inserted row and you don't already have a timestamp or sequence column, you need to add one.
HTH
David

Similar Messages

  • Retrive a record before the last inserted record

    Hi,
    I use simple select statement to retrive records where cola = 'Yes':
    select * from tbl1
    where cola = 'Yes';
    The result gives me about 10 records with different date.
    From the above result, I want to get the record right before the last inserted record according to the date field; which means I can not use MAX function (all I want just one record).
    If I write a function to return a date and passes it back to the SELECT statement, I'll get a date for each record, which I don't want.
    I know there's a way to do that in PL/SQL.
    Plese help me.
    pem

    select * from (
    select * from
    (select * from (select * from (table> order by <date> desc) where rownum < 3)
    order by <date>) where rownum =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

  • How to find last inserted record in the table.

    Version: Oracle 10g
    I have a table called "Manufacture" and 3 columns as mfno,itemname,quantity.
    How to find last inserted record in the table "Manufacture".
    As i come to know that Rowid is not result perfect results. Please provide your inputs.

    user13416294 wrote:
    Version: Oracle 10gThat is not a version. That is a product name. A version is 10.1.0.2 or 10.2.0.4, etc.
    I have a table called "Manufacture" and 3 columns as mfno,itemname,quantity.
    How to find last inserted record in the table "Manufacture".Not possible as your data model does not cater for it. That simple.
    If there is a need to determine some order or associate some time to an entity, then that should be part of the data model - and a relationship, or one or more attributes are needed to represent that information. Thus your data model in this case is unable to meet your requirements.
    If the requirements are valid, fix the data model. In other words - your question has nothing to do with Oracle and nothing to do with rowid, rowscn or other pseudo columns in Oracle. It is a pure data modeling issue. Nothing more.

  • Fatch last inserted record in ztable

    hi all,
    any buddy can tell me how can i get last inserted record from my ztable.

    Hi,
    I think this wont work .....
    Ex: table has   (considering first two cols as PK's)
    000123   AA   124
    000123   AB   124
    000123   AC   124
    000123   BA   124
    000123   BB   124
    000123   BC   124
    000123   LH   124
    Now i am inserting         000123   BD   124
    => Records will be arranged as
    000123   AA   124
    000123   AB   124
    000123   AC   124
    000123   BA   124
    000123   BB   124
    000123   BC   124
    000123   BD   124
    000123   LH   124
    How will yur query works ? Please clarify ....

  • GET LAST INSERTED RECORD

    I am fairly new to flex. So please bear with me no this
    question.
    I would like to get MYSQL ID of the last record I inserted.
    MSQL has as function LAST_INSERT_ID() that is connection based and
    will return the last id of the last inserted record. My thought was
    in the function contained within the cfc to do 2 queries insert the
    record and return the last id directly after (see below). However,
    I don't know how I would read the results back into flex. I am
    unsure how to write the event handler. Thanks
    <cffunction name="insertUser" access="remote"
    returntype="query" >
    <cfargument name="SSN" required="false" type="string"
    hint="SSN"/>
    <cfargument name="FNAME" required="false" type="string"
    hint="FNAME"/>
    <cfargument name="LNAME" required="false" type="string"
    hint="LNAME"/>
    <cfargument name="CLIENTID" required="false"
    type="string" hint="NEW CLIENTID"/>
    <cfquery name="insUser" datasource="DBT">
    INSERT INTO CLIENT (SSN, FNAME, LNAME) VALUES (
    <cfqueryparam value="#ARGUMENTS.SSN#"
    cfsqltype="CF_SQL_VARCHAR" maxlength="12"/>,
    <cfqueryparam value="#ARGUMENTS.FNAME#"
    cfsqltype="CF_SQL_VARCHAR" maxlength="20"/>,
    <cfqueryparam value="#ARGUMENTS.LNAME#"
    cfsqltype="CF_SQL_VARCHAR" maxlength="30"/>)
    </cfquery>
    <cfquery name="qRead" datasource="DBT">
    SELECT last_insert_id() AS CLIENTID
    </cfquery>
    <cfreturn qRead >
    </cffunction>

    I assume you have LCDS installed with CF and you are using
    the remote object service,
    <mx:RemoteObject
    id="myService"
    destination="ColdFusion"
    source="full.path.to.cfc"
    showBusyCursor="true">
    <mx:method name="insertUser"
    result="insertUser_result(event)" />
    </mx:RemoteObject>
    <mx:Scrpit>
    private function insertUser_result(event:ResultEvent) {
    trace(event.result);
    event.result will be the id of the newly inserted record.
    BTW, I would highly recommend you looking into the data
    management servcie. the SQLAssembler can spare you of the CF code
    for simple CRUDs like this.

  • How to get the last inserted record from a table ?

    :-) Hiee E'body
    I work on Oracle 8i and need to get the last
    record inserted in a table.
    I have tried using rownum and rowid pseudo-columns
    but that doesn't work.
    Can you please help me out ?
    :-) Have a nice time
    Vivek Kapoor.
    IT, Atul Ltd.,
    India.

    I'm not sure about 8i features.
    I assume here that you don't have 'Date-Time' stamp columns on the table which is the easiest way to determine the last inserted row in the table.
    If not try the following :-
    select address, piece, SQL_TEXT
    from V$SQLTEXT
    where upper(sql_text) like '%INSERT INTO TABLE_NAME%'
    Substiute the TABLE_NAME with the name of the actual table.
    Have fun.
    Regards,
    Shailender
    :-) Hiee E'body
    I work on Oracle 8i and need to get the last
    record inserted in a table.
    I have tried using rownum and rowid pseudo-columns
    but that doesn't work.
    Can you please help me out ?
    :-) Have a nice time
    Vivek Kapoor.
    IT, Atul Ltd.,
    India.

  • Oracle - Last inserted record

    hello
    I need select last added/inserted records where value = 'test1' and = 'test2' and = 'test3' in column 'NAME'
    should be one record for each 'test1','test2' ...
    aslo..table have column 'DATE_USR' with data_type = timestamp(6)
    thanks for help
    Edited by: user8855885 on 2012-08-07 02:00
    Edited by: user8855885 on 2012-08-07 02:01
    Edited by: user8855885 on 2012-08-07 02:01

    /* Formatted on 8/7/2012 4:56:36 PM (QP5 v5.139.911.3011) */
    WITH t AS (SELECT 99 id,
                      'test1' name,
                      '12/08/03 11:38:43' dt,
                      'ok' DES
                 FROM DUAL
               UNION ALL
               SELECT 24,
                      'test2',
                      '12/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 97,
                      'test2',
                      '11/07/01 11:38:43',
                      'ok'
                 FROM DUAL
               UNION ALL
               SELECT 55,
                      'test2',
                      '11/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 43,
                      'test3',
                      '12/07/01 11:38:43',
                      'ok'
                 FROM DUAL
               UNION ALL
               SELECT 88,
                      'test3',
                      '10/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 34,
                      'test4',
                      '12/07/01 11:38:43',
                      'nok'
                 FROM DUAL
               UNION ALL
               SELECT 44,
                      'test4',
                      '09/07/01 11:38:43',
                      'nok'
                 FROM DUAL)
    SELECT *
      FROM (SELECT t.*,
                   RANK ()
                   OVER (PARTITION BY name
                         ORDER BY TO_DATE (dt, 'dd/mm/yy hh:mi:ss') DESC)
                      rk
              FROM t) x
    WHERE 1 = rkoutput:
    99     test1     12/08/03 11:38:43     ok     1
    24     test2     12/07/01 11:38:43     nok     1
    43     test3     12/07/01 11:38:43     ok     1
    34     test4     12/07/01 11:38:43     nok     1

  • CREATING A TRIGGER THAT PICKS UP THE LAST INSERTED RECORD

    Hello everybody
    my question. is it possible to embed a sql query whitin a trigger? the thing is that when I have to insert the value, this actually would have to be retrieved from another table. It is an AFTER INSERT, but it needs to know which name I inserted in table 1 in order to also insert it into table 2.
    More specifically. Imagine a table with usernames and passwords that I populate as I set them up
    Then I need a trigger that as it sees that I have entered that username it takes it and populates another table with that plus a generated sequence that could be the result of some string function
    CREATE OR REPLACE
    TRIGGER GENERATE_SEQUENCE
    AFTER INSERT ON USERS_TABLE
    FOR EACH ROW
    BEGIN
    insert into GENERATED_SEQUENCES_TABLE (lastinserted_name,surrogate_key)
    values(SELECT USERNAME FROM USERS_TABLE WHERE USERID....., SUBSTR (lastinsertedname¦¦ password), 1,12);
    END;
    so, this lastinserted name is what I am trying to retrieve. I mean if it is not madness to insert a sql query in the way I have done it, I could write in the query to select the highest ID from the PK and that would give me the last inserted name.
    thanks a lot
    Alvaro

    In an insert trigger you can reference all the inserting values with :new.column_name, hence there's no need to select from the USERS_TABLE table:
    CREATE OR REPLACE
    TRIGGER GENERATE_SEQUENCE
    AFTER INSERT ON USERS_TABLE
    FOR EACH ROW
    BEGIN
    insert into GENERATED_SEQUENCES_TABLE (lastinserted_name,surrogate_key)
    values(:new.USERNAME, ...);
    END;
    /Max
    [My Italian Oracle blog|http://oracleitalia.wordpress.com/2010/02/05/gestione-degli-errori-in-sql-con-log-errors/]

  • Fetching the last inserted record

    How can I fetch the last record inserted into oracle table?

    Hi Waleed / Bhavesh
    In pure RDBMS terms there is no concept of a first / last record. Also the query that Bhavesh has give makes an assumption that emp_n have been sequentially or inserted in some form of continuous incremental format.
    It is not adviseable to use ROWNUM and ORDER BY together since Oracle allocates ROWNUMs before it actually orders the data

  • How can I get the last / newest record of a table?

    Hi,
    I am developing an ecard app that has a create greeting page, a preview page with edit and send buttons and an edit page.
    I got he insert working, and on the preview page I created a master record page (displays all records), and delete all unecessary designs. Doing it this way also gives me the 'edit' button, so people are able to edit their page.
    How do I get only the last record displayed though? At the moment it loops through all records and displays them in sets of 10. I found the variable that holds the totla count of records ('$totalRows_rs7417_content1'), but how do I (re-)write the script so it ONLY diplays the last record?
    I need to get the ID of this record as I am writing this into the PARAMs of the object and embed tags of my message .swf (this picks up the ID and based on that sucks the greetings text out of the database via another php script.
    Also, from this page, how can I send an email to the sender (ie creator of the message) as well as the recipient? Both email addresses are in the databse so should be part of the erecord returned.
    Thanks,
    Nik

    -----
    OK, So how do I recreate what I got in a non-ADDT list?
    well, when you´re just about to display one certain record, the term "list" is somewhat inappropriate anyway.
    What you´d simply need to do IMHO, is using DW´s native functions to create the basic "SELECT * FROM table_name ORDEr BY ID DESC" query, add "LIMIT 1" manually, and display the desired "dynamic text" placeholders in 1 table cell -- don´t think there were any need for all the fancy stuff
    (sorting, filtering etc) provided by ADDT´s list, as it´s just 1 record.
    It also occurred to me just now that finding the last record that has been added *may-* notbe enough if the site gets used a lot by our executives (I am thinking of a situation where 2 or three peolpe create greetings at the same time and press submit and then getting each others messges rather than their own).
    in this case you might want to additionally store the respective executive´s "kt_login_id" session variable -- because it´s this unique "identifier" which should be used to show the last inserted record of the person who actually inserted it, means adding a "WHERE submitter_id" equals the session variable kt_login_id - clause to the query.
    When creating a non-ADDT list that´s at some point based on detecting a user session, you´ll BTW need to insert at line 1
    I can't seemt o insert it on the page as it is looking for a insert, update or delete bahaviour on the page which of course I don't have
    please search these forums for several posts I made which explain my "use dummy table" approach that´s made for cases when you actually don´t have anything to insert/update/delete, but need "something" for ADDT´s send email behaviours.
    Cheers,
    Günter Schenk
    Adobe Community Expert, Dreamweaver

  • Getting the last insert id

    Hi there,
    I'm trying to get the id of the last inserted record. Normally would use $last_id = mysql_insert_id() after the insert statemant but I can't work out how to get this value when I use a ADDT insert transaction.
    Any help much appreciated.
    Cheers

    I´m locking this thread because the OP has additionally added a related question to an existing thread, and a solution to this problem has been provided there too: using  mysql_insert_id() with the ADDT

  • How to get the last inserted Autoincrement value in Java for Pervasive DB

    Hi, I need to get the last inserted auto incremented value after the record is inserted IN JAVA For ex. consider we have 4 columns for the myTable in the PERVASIVE DATABASE V10 autoid - identity column (auto increment column) userID userName pageID insertSqlExpression = insert into myTable (userID , userName, pageID) values( ? ,? ,?); prepareInsert = connection.prepareStatement(insertSqlExpression); prepareInsert .excuteUpdate; After inserting the new record how can I get the autoid value (last inserted value) in the Java. Please help me.
    Thanks in advance. Arthik

    JavaArthikBabu wrote:
    I dont have privileges to write new stored procedures in database.I need to do in the Java side only. In many databases that irrelevant. The same way you would do it in a proc can be done with the correctly conceived statement (singular.)
    For ex &#150; if we insert and then the select record's identity value as a single transaction and would this guarantee that what is returned by the select would not include inserts that another might have made?Please do not take that path unless you are absolutely certain that your database does not support any other way to do it.

  • Simple Query Question - How do I return the Last 3 records of a Table?

    Question.
    For example, I have a table that has 50 records.
    How do I, specify in SQL to only return the last 3 records of the table.
    Select a.* from table a where ????

    I was just trying to show an example to a friend on
    how something like this would work and if it was even possible. But it won't work. Here's a simple example:
    SQL> create table emp
      2  (id)
      3  as
      4  select object_id
      5  from   all_objects
      6  order  by object_id;
    Table created.
    SQL> select *
      2  from  (select rownum rn
      3               ,b.*
      4         from   emp b)
      5  where  rn > ( select (max(rownum) - 3)
      6                from    emp)
      7  ;
            RN         ID
         40830      55891
         40831      55892
         40832      55893So far, so good. These are the "last 3" rows inserted. Now delete a bunch of rows and insert 3 new ones:
    SQL> delete emp where id < 40000;
    33423 rows deleted.
    SQL> commit;
    Commit complete.
    SQL> insert into emp values (60000);
    1 row created.
    SQL> insert into emp values (60001);
    1 row created.
    SQL> insert into emp values (60002);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select *
      2  from  (select rownum rn
      3               ,b.*
      4         from   emp b)
      5  where  rn > ( select (max(rownum) - 3)
      6                from    emp)
      7  ;
            RN         ID
          7410      55891
          7411      55892
          7412      55893Here's the problem. Even though the "last 3 rows" are 60000 - 60002, I still get the same ones as the first query.

  • Inserting records in the table

    Hi All,
    I am working on a report where we are inserting huge numbers of records from an internal table to the ztable, I have been advised that rather than inserting all the records at one time we should divide the recods into small blocks may be let's say 100,000 of each block and try inserting the blocks in the ztable. So I was just wondering if anyone of you can help me with this how can I get this?
    my internal table is it_data_table and my ztable is z_data_table.
    Thanks,
    Rajeev

    Hi,
    Rather than inserting records from the workarea it is better if you use the inetrnal table for insertion. If youu have say 50025 records than it will hit the database that many times which definitely is a performance issue.If you have an internal table say 'it' and another internal table 'it_temp' with a similiar structure, you can try something like as follows
    loop at it.
      count = count + 1.
      it_temp = it.
      append it_temp.
      if count = 10000.
        insert ztable from table it_temp accepting duplicate keys.  "to avoid a dump if there are duplicates count = 0.
        refresh it_temp.
      endif.
    endloop.
    if it_temp is not initial.
      insert ztable from table it_temp accepting duplicate keys.
    endif.
    In the above code the loop will run the number of record times but, the insert statement will run only 50025/10000 times (i.e, 5 times). After the completion of the loop we need to add the remaining 25 records (because insert will work last inside the loop when 50000 records are reached), for that we will issue the insert statement after completion of the loop, after checking the it_temp table. So the total of 50025 records would be entered in a batch of 6 inserts rather than that many inserts if using just the workarea.
    I hope this does help you in your issue.
    Thanks and Regards,
    Sachin.

Maybe you are looking for

  • Imac wont boot from cd, Can anyone help me Please

    Hello, I just bought my first mac from a co-worker. it didnt have a hard drive so i threw a spare one i had in. I tried holding c when booting, and all i get is the little smile face and the ? on a folder. At first when i booted the computer it didnt

  • Can't copy or move iPhoto library because file is in use.

    I'm trying to copy my iPhoto library which is stored on an external drive, to my new Mac Mini. The external drive has been connected to my laptop, and the file has been my primary library for the laptop, which is a MacBook Pro running Mountain Lion,

  • How to integrate Explorer in a Java panel

    Hello. I beg your pardon if I'm asking something that has already been asked... I'm new in this forum. My question is simple... I'd like to put an internet explorer in my java applications... How can I get it? sorry for my bad english :) Marcus

  • Is my 37 month old MacBook Battery ready to be replaced?

    I run primarily on AC (with battery installed of course). But I may need to run on battery a bit in the coming weeks, and I see my MB (May 2006) battery status as this: Charge Information: Charge remaining (mAh): 1463 Fully charged: Yes Charging: No

  • Paragraph styles indesign

    assigned font in paragraph style does not assign to test when style is applied