latest row inserted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitbasu77
    New Member
    • Feb 2008
    • 89

    latest row inserted

    Hi friends,

    how to see all the inserted rows of a particular date of a table.

    regards
    rohit
  • rohitbasu77
    New Member
    • Feb 2008
    • 89

    #2
    ok.......need help

    thanks
    bye

    Comment

    • amitpatel66
      Recognized Expert Top Contributor
      • Mar 2007
      • 2358

      #3
      Originally posted by rohitbasu77
      ok.......need help

      thanks
      bye
      For latest row, you can use MAX(ROWID)

      Comment

      • rohitbasu77
        New Member
        • Feb 2008
        • 89

        #4
        Originally posted by amitpatel66
        For latest row, you can use MAX(ROWID)

        where ROWID > 20

        doesn't meant that the selected rows are inserted by today

        Comment

        • amitpatel66
          Recognized Expert Top Contributor
          • Mar 2007
          • 2358

          #5
          Originally posted by rohitbasu77
          where ROWID > 20

          doesn't meant that the selected rows are inserted by today
          If you use ROWID > 20 in your WHERE condition, the query will error out and will not work. I suggested you to make use of ROWID = MAX(ROWID) to get the last record inserted in to the table.

          Comment

          • Saii
            Recognized Expert New Member
            • Apr 2007
            • 145

            #6
            Do you have any date column in your table that stores this information?

            Comment

            • PelleFork
              New Member
              • Mar 2008
              • 3

              #7
              If you are running 10g or higher, you can use the ORA_ROWSCN pseudocolumn, which can be translated to a timestamp using the SCN_TO_TIMESTAM P function.

              Note, however, that data that was imported from another instance have invalid SCN:s and will raise an exception. So, I wrote this little function that has proven quite handy; it just returns NULL whenever there's an imported row with an invalid SCN.

              Code:
              CREATE OR REPLACE function rowdate( rowscn in number )
              return date
              as
                l_rowts  timestamp;
                bad_scn  exception;
                pragma exception_init( bad_scn, -8181 );
              begin
                     l_rowts := scn_to_timestamp( rowscn );
                     return cast( l_rowts as date);
                     exception when bad_scn then
                                   return null;
              end;
              /
              So, a "select * from mytab where rowdate( ora_rowscn ) > trunc( sysdate )" will return today's rows.

              Note that this will give you not only rows inserted today, but also the updated ones.

              Comment

              • rohitbasu77
                New Member
                • Feb 2008
                • 89

                #8
                Originally posted by Saii
                Do you have any date column in your table that stores this information?
                find pelle's reply and try to do more with SCN....
                that what the result is....

                Comment

                • rohitbasu77
                  New Member
                  • Feb 2008
                  • 89

                  #9
                  Originally posted by PelleFork
                  If you are running 10g or higher, you can use the ORA_ROWSCN pseudocolumn, which can be translated to a timestamp using the SCN_TO_TIMESTAM P function.

                  Note, however, that data that was imported from another instance have invalid SCN:s and will raise an exception. So, I wrote this little function that has proven quite handy; it just returns NULL whenever there's an imported row with an invalid SCN.

                  Code:
                  CREATE OR REPLACE function rowdate( rowscn in number )
                  return date
                  as
                    l_rowts  timestamp;
                    bad_scn  exception;
                    pragma exception_init( bad_scn, -8181 );
                  begin
                         l_rowts := scn_to_timestamp( rowscn );
                         return cast( l_rowts as date);
                         exception when bad_scn then
                                       return null;
                  end;
                  /
                  So, a "select * from mytab where rowdate( ora_rowscn ) > trunc( sysdate )" will return today's rows.

                  Note that this will give you not only rows inserted today, but also the updated ones.

                  Thanks Pelle .....
                  Its a good reply....

                  Comment

                  • rohitbasu77
                    New Member
                    • Feb 2008
                    • 89

                    #10
                    Originally posted by PelleFork
                    If you are running 10g or higher, you can use the ORA_ROWSCN pseudocolumn, which can be translated to a timestamp using the SCN_TO_TIMESTAM P function.

                    Note, however, that data that was imported from another instance have invalid SCN:s and will raise an exception. So, I wrote this little function that has proven quite handy; it just returns NULL whenever there's an imported row with an invalid SCN.

                    Code:
                    CREATE OR REPLACE function rowdate( rowscn in number )
                    return date
                    as
                      l_rowts  timestamp;
                      bad_scn  exception;
                      pragma exception_init( bad_scn, -8181 );
                    begin
                           l_rowts := scn_to_timestamp( rowscn );
                           return cast( l_rowts as date);
                           exception when bad_scn then
                                         return null;
                    end;
                    /
                    So, a "select * from mytab where rowdate( ora_rowscn ) > trunc( sysdate )" will return today's rows.

                    Note that this will give you not only rows inserted today, but also the updated ones.
                    yes pelle, that will work. can you check with this one:

                    select * from mytab where scn_to_timestam p( ora_rowscn ) - 0 > sysdate - 1

                    The function "scn_to_timesta mp" returns "systimesta mp" type data.
                    And if you do:
                    select systimestamp - 1 from dual
                    it returns sysdate type data.

                    Comment

                    • manojkmaurya
                      New Member
                      • Jan 2012
                      • 1

                      #11
                      ORA_ROWSCN is not same as SCN

                      Thanks for the tip.

                      ORA_ROWSCN of very old row might be too old to be a valid SCN.
                      In that case, using scn_to_timestam p will give error:

                      ORA-08181: specified number is not a valid system change number
                      ORA-06512: at "SYS.SCN_TO_TIM ESTAMP", line 1

                      But, in case of a very recent row, this will work.

                      Originally posted by rohitbasu77
                      yes pelle, that will work. can you check with this one:

                      select * from mytab where scn_to_timestam p( ora_rowscn ) - 0 > sysdate - 1

                      The function "scn_to_timesta mp" returns "systimesta mp" type data.
                      And if you do:
                      select systimestamp - 1 from dual
                      it returns sysdate type data.

                      Comment

                      Working...