Solved

SQL Server 2005 DELETE Query

Posted on 2014-10-10
5
229 Views
Last Modified: 2014-11-24
I have a sql server 2005 database that has one table in particular with approx. 800,000 rows. I need to delete all but one row of this table for troubleshooting purposes.

Looking at the table there appears to be a column called 'SEQ' which contains numbers that appear to be unique to each row. I wish to pick one of these numbers randomly and delete all rows except that one.

What would be the best way to achieve this goal?
0
Comment
Question by:dowhatyoudo22
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40373065
it depends on what you call "best"

if this is a test table, just rename it, recreate it with the old name, and copy just 1 record back. this should assign it a new SEQ number , unless you want to keep the "exact" value, in which case: http://technet.microsoft.com/en-us/library/aa259221%28v=sql.80%29.aspx

to get 1 random record:
select top 1 * from yourtable order by newid()
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 40373073
Assuming there is no referential-integrity that would prevent you from deleting a record:
-- grab the record(s) into a temporary table
SELECT * INTO #temp FROM YourTable WHERE something = whatever

-- delete all rows, avoiding transaction-log
TRUNCATE TABLE YourTable

-- insert the record(s) captured earlier
INSERT YourTable SELECT * FROM #temp

Open in new window

0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 40373081
You can save a record randomly (use Guy's suggestion) and the TRUNCATE the table (deletes all records without logging) and then put back the saved record:
SELECT TOP 1 * 
INTO #tempTable
FROM yourTable 
ORDER BY newid()

TRUNCATE yourTable

INSERT INTO yourTable
SELECT * FROM #tempTable

DROP #tempTable 

Open in new window

0
 
LVL 24

Expert Comment

by:Mohammed Khawaja
ID: 40373413
I agree with Vitor, his solution is elegant and easy to deploy.  If it makes you feel better, you could run the query "select top 1  * from yourtable", copy the information to notepad or save the results to a file and then truncate the table.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40373581
For the record, truncating a table does *not* "avoid logging", it reduces it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now