Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to delete last row in a table

Author  Topic 

Mister
Starting Member

4 Posts

Posted - 2007-09-10 : 21:04:53
Hello - I inserted a row and now need to delete it. Is there a simple query to delete the last row inserted into a table?

Thanks for your help!

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-10 : 21:09:57
Do you not know what you inserted?



Future guru in the making.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-09-10 : 21:36:48
Do you have a date time column in the table that record when a row was inserted ? or do you have identity column ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Mister
Starting Member

4 Posts

Posted - 2007-09-10 : 22:22:54
I entered data into several columns in the row using the Insert Into function. The dates I entered did not insert in the correct format. I guess I could just update the columns in that row, but I'm not sure how to get the correct date format.
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-10 : 22:30:51
quote:
Originally posted by Mister

I entered data into several columns in the row using the Insert Into function. The dates I entered did not insert in the correct format. I guess I could just update the columns in that row, but I'm not sure how to get the correct date format.



What format do you need the date in?



Future guru in the making.
Go to Top of Page

Mister
Starting Member

4 Posts

Posted - 2007-09-10 : 22:35:13
quote:
Originally posted by Zoroaster

quote:
Originally posted by Mister

I entered data into several columns in the row using the Insert Into function. The dates I entered did not insert in the correct format. I guess I could just update the columns in that row, but I'm not sure how to get the correct date format.



What format do you need the date in?



Future guru in the making.



I entered 11/01/07 and 1900-01-02 00:00:00.000 was entered. So would that be DateTime?
Go to Top of Page

Vijaykumar_Patil
Posting Yak Master

121 Posts

Posted - 2007-09-11 : 01:16:45
That is because you missed the year..you would need to enter 2007 and not 07. That is the cause of your problem. And in regards to format look at BOL convert and cast functions.

Necessity is the mother of all inventions!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-11 : 01:35:13
"Is there a simple query to delete the last row inserted into a table?"

Well, in a relational database there isn't really any concept of "the last row inserted". The data is in no particular order unless you use an ORDER BY to sort it (when you Select it), or you specify explicitly some criteria in a WHERE clause.

So if you have some Unique characteristics of the record you added then you can Delete it.

So try something like

SELECT *
-- DELETE D
FROM MyTable AS D
WHERE MyDate = '19000102 00:00:00.000'

and see if that displays the record you added. Once you display the required record (and only that one!) then remove the SELECT line and remove the comment from the DELETE line and you can delete it. Check that the delete says that it processed ONLY ONE ROW! Otherwise restore from backup. (Make a backup first pls!)

Kristen
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-11 : 01:36:11
"That is because you missed the year..you would need to enter 2007 and not 07"

That's news to me!! unless I misunderstood what you meant?

SELECT CONVERT(datetime, '11/01/07')

gives me:

2007-11-01 00:00:00.000

which looks about right to me.

Kristen
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-11 : 01:47:16
He missed the leading and trailing ' and thus 11/01/07 is treated like math

11/01 = 11
11/07 = 1

1 equals '19000102 00:00:00'



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-11 : 01:51:33
"He missed the leading and trailing ' "

Ah! That would do it ...
Go to Top of Page
   

- Advertisement -