Solved

How to locate nth item in an MSSQL table

Posted on 2009-05-11
8
245 Views
Last Modified: 2012-06-27
I have a log table. Once table has > 10000 row in the table I need to remove earliest rows created until table contains no more than 10000 rows.

I am attempting to create delete query that will meet this requirement.

Have attempted with:
delete from logtable where pkeyid < (select top(1) pkeyid from
(select top (10000) pkeyid from logtable)  order by pkeyid desc))

but MSSql report syntax errors for this attempt

Looking for corrections to my query or alternate providing same solution results
0
Comment
Question by:JoeSnyderJr
  • 4
  • 4
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24355533
this should do:
delete from logtable 
where pkeyid in (select top(10000) pkeyid from logtable order by pkeyid desc)

Open in new window

0
 

Author Comment

by:JoeSnyderJr
ID: 24355629
Sorry but that is going to delete the most current 10000 records. I am attempting to delete records NOT in that set.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24355656
delete from logtable
where pkeyid not in (select top(10000) pkeyid from logtable order by pkeyid desc)
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:JoeSnyderJr
ID: 24355754
Granted this will work but won't this be a resource hog compared to delete where pkeyID < TOP(1)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24355866
that can only be answered by checking the explain plan vs the timing of the queries under similar environments...

your syntax error, btw, was here:
delete from logtable 
  where pkeyid < ( select top 1 pkeyid 
                     from ( select top 10000 pkeyid 
                              from logtable order by pkid desc 
                          ) sq  
                     order by pkeyid asc
                  ) asc

Open in new window

0
 

Author Comment

by:JoeSnyderJr
ID: 24355980
Sorry to be a pain but don't recognize meaning for seqment ') sp' and get syntax error on line 7 near 'asc'
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24356094
ousp
delete from logtable 
  where pkeyid < ( select top 1 pkeyid 
                     from ( select top 10000 pkeyid 
                              from logtable order by pkid desc 
                          ) sq  
                     order by pkeyid asc
                  ) 

Open in new window

0
 

Author Closing Comment

by:JoeSnyderJr
ID: 31580181
PERFECT!  Thanks much for prompt assistance. No wonder you are consistently at top of assistance list.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

839 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