Solved

SQL Server 2005 DELETE Query

Posted on 2014-10-10
5
232 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 46

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 25

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

911 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

21 Experts available now in Live!

Get 1:1 Help Now