Solved

Delete by Row SQL Syntax

Posted on 2007-03-29
7
1,391 Views
Last Modified: 2011-10-03
I'm trying to clean up a database and delete the last 700 rows, from 9270 to 9770.  How whould I call the rows to delete?

I could use this:
Delete from Companies where ID > 9270;

the ID's are not in order though.  I need to delete by row.  

Delete from Companies where (row) > 9270;

Please help thanks.
0
Comment
Question by:smoothcat11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18818230
>>last 700 rows

Last, ordered how?

Sequence is not something guaranteed in a relational DB -- except when you use an ORDER BY.

Once you determine what ORDER BY puts those 700 last, we can talk about the criteria to delete those rows.  It will probably be WHERE someField > Something -- and that will be apparent once you determine the ordering.
0
 

Author Comment

by:smoothcat11
ID: 18818311
It's ordered by the INSERT.  So if you add the record by a form it will put the last insert into the database.  Also, some of the records were deleted so the id is all over the place.  I know I have to delete the last 700 records that were inserted and I don't know the syntax to delete the last rows or even call a record by the row.  Thanks
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18818449
Is it the ID that is between 9270 and 9770?

I get the idea it's not that easy ...

OK, what if you add a field with an autonumber / identity in it.  Does that come out in the order you want?  if so ... you're home free.
0
 

Author Comment

by:smoothcat11
ID: 18818722
I exported it to Excel then took out the last rows.

create temporary table temp (id int not null primary key auto_increment, data varchar(255)) select null, data from _template;
select * from temp;
drop table temp;

I got that from someone.  You can get it in order w/ that and then delete anything w/ ID > 9270

Thanks.
0
 
LVL 1

Accepted Solution

by:
SignsUS earned 500 total points
ID: 22087749
You almost have it:

Delete from Companies where ID >= 9270 AND ID <= 9770;

Done :)

It doesn't matter what order they are in.  It will delete according to those conditions.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL Query - Group By Year 3 55
SQL Function 4 38
Create a Calendar table 29 45
T-SQL: Number of Records is Greater Than One 7 51
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question