how to delete last inserted row in sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumit anand
    New Member
    • Sep 2014
    • 1

    how to delete last inserted row in sql server

    how to delete last inserted row in sql server
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You would need an auto increment field or a date field that defaults to when the row is inserted.

    Comment

    • mukherjee
      New Member
      • Sep 2014
      • 9

      #3
      delete top 1 from <table>.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Mukherjee, your code will not work.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3653

          #5
          sumit anand,

          You would need a way to know which record was the last inserted. Rabbit's suggestion of an auto-increment field will tell you the most recent, based on the record with the largest value.

          @mukherjee, your method willonly delete the first record the query finds, which could be just about any record, depending upon how the query is written.

          Comment

          Working...