November 26, 2012 at 5:11 am
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
November 26, 2012 at 5:23 am
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
November 26, 2012 at 9:17 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 26, 2012 at 9:35 am
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-2 INT
SET @C = 1
SET @b-2 = 1
WHILE @C < 101
BEGIN
INSERT INTO Unk
SELECT @b-2,'abc'
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.
November 27, 2012 at 3:20 am
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]'
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply