Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Delete records Expand / Collapse
Author
Message
Posted Monday, November 26, 2012 5:11 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 5:50 AM
Points: 63, 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
Post #1388545
Posted Monday, November 26, 2012 5:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 3, 2014 1:55 PM
Points: 199, Visits: 735
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

Post #1388548
Posted Monday, November 26, 2012 9:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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.

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 Moden's 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)
Post #1388678
Posted Monday, November 26, 2012 9:35 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, October 14, 2014 10:58 AM
Points: 5,333, Visits: 25,272
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
Post #1388686
Posted Monday, November 26, 2012 3:08 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 1:27 PM
Points: 1,945, Visits: 3,068
I have table with 20k records.


Rows are not records. SQL is a set oriented language, so we do not like to use counts of sequentially ordered, physical units. When you say "I don't know the table structure", you are back to working with a deck of punch cards. We would put the deck into a card sorter, turn on the counter and stop it when we had the top of the deck!

But its failed(Should mention the column name in order by clause).


Yes, you should. The proprietary TOP(n) without an ORDER BY will produce random results. You do not know the right terms and have no concept of set oriented declarative programming. What are you actually trying to do?


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1388868
Posted Tuesday, November 27, 2012 3:20 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:55 AM
Points: 2,873, Visits: 5,185
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1389049
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse