SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete records


Delete records

Author
Message
Sony Francis @EY
Sony Francis @EY
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 293
I have table with 20k records. I want to delete 20k-1000 records from the table. i.e keep the top 1000 records and rest of them are need to be deleted. I dont knw the table structure. I have tried using cte and row_number () . But its failed(Should mention the column name in order by clause).


Is any body to help me write a single query for it
WolfgangE
WolfgangE
SSC-Addicted
SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)SSC-Addicted (473 reputation)

Group: General Forum Members
Points: 473 Visits: 792
hi,

for making the delete more performant you should use a loop, something like that:

delete top ( 100000 ) from myTable
while @@rowcount > 0
begin
delete top ( 100000 ) from myTable
end



For leaving some rows in the table there might be serveral ways. One could be querying count(*) of the table each time and breaking the loop but I think this would slow down the whole process a lot.
Is there any ascending ID you can query? Then you could extend the query something like this:


declare @rowcount bigint = 1;
declare @MaxId bigint;
select @MaxId = max(x.id) from ( select top 1000 Id from myTable ) as x

while @rowcount > 0
begin
delete top ( 100000 ) from myTable where Id > @MaxId
set @rowcount = @@rowcount;
end


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25805 Visits: 17509
Sony Francis @EY (11/26/2012)
I have table with 20k records. I want to delete 20k-1000 records from the table. i.e keep the top 1000 records and rest of them are need to be deleted. I dont knw the table structure. I have tried using cte and row_number () . But its failed(Should mention the column name in order by clause).


Is any body to help me write a single query for it


What is the top 1000? There is no concept of order in a table. Since there are only 20k rows to be deleted I doubt that batching will help a lot, unless the rows are really large.

You could do this fairly easily with a cte.


;with MyData as
(
select ROW_NUMBER() over(order by SomeField) as RowNum, * from YourTable
)

delete MyData where RowNum > 1000



If you need more specific help you are going to have to provide more specific information (ddl, sample data, desired output). See the first link in my signature for best practices when posting questions.

I am horrified by your comment that you are deleting all but 1,000 rows out of a table and you don't even know the structure of the table. w00t

It sounds like you have tried something but simply saying "it failed" does not provide any kind of information. What happened? Did you get an error message?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7769 Visits: 25280
Here is a method that you might want to test to verify it does what you require, and if it does test AGAIN and AGAIN to be certain, before attempting in a production system
--Code to create your table whose structure is NOT known and to demonstrate a possible path for you.
CREATE TABLE Unk(Col1 INT,Col2 VARCHAR(3))
DECLARE @C INT
DECLARE @B INT
SET @C = 1
SET @B = 1
WHILE @C < 101
BEGIN
INSERT INTO Unk
SELECT @B,'abc'
SET @B = @B + 1
SET @C = @C + 1
END


-- Now a method to take the data from this unknown structure and insert it into a new table with the same structure

  -- Select from table whose structure is NOT known
-- Into a new table Note the first select statement
-- creates a new table which I named Kn
SELECT TOP(10)* INTO Kn FROM Unk ORDER BY Col1 ASC
--Check that the NEW table exists and contains the required number of rows
-- which in this example should be 10
SELECT COUNT(*) FROM Kn
-- now rename the old and "new" tables
sp_RENAME 'Unk','Junk'
sp_RENAME 'Kn', 'Unk'
-- now check again
SELECT COUNT(*) FROM Unk


Do not drop or delete data from the old existing table (now named Junk) until you are CERTAIN the results are what is required.

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4954 Visits: 5478
Sony Francis @EY (11/26/2012)

... I dont knw the table structure.
...



If you don't know the table structure, how are you going to write a query against it?
You can easily review table structure using

EXEC sp_help [tablename]

If your table is not from dbo schema, use this:

EXEC sp_help '[schemaname].[tablename]'

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search