Solved

DELETE TOP syntax

Posted on 2003-10-22
9
976 Views
Last Modified: 2008-02-01
I was trying to do something like

DELETE TOP 3 FROM tblName

But the query anaylzer says invalid keyword by the word TOP.

I was going to use the SET ROWCOUNT method, such as

SET ROWCOUNT = 3
DELETE FROM tblName

But SQL Books Online suggested that you should replace any SET ROWCOUNT commands with the appropriate TOP version for any DELETE, INSERT, UPDATE.

So according to it, there is a DELETE TOP command, but I can't seem to find a reference anywhere on what the syntax is. Does anyone know if this is a valid command, and if so what the syntax is? By the way I'm using MS SQL 2000, thanks.
0
Comment
Question by:Jeffbub213
  • 3
  • 3
  • 3
9 Comments
 

Author Comment

by:Jeffbub213
ID: 9598919
Also, on a similar note, I tried using a dynamic query in the WHERE clause, but I guess this isn't allowed?

declare @numRows int                  --This would be an input parameter to a stored procedure actually
SET @sqlStr = 'SELECT TOP ' + CONVERT(varchar(2), @numRows) + ' field1 FROM tblName'
DELETE FROM tblName WHERE field1 IN (EXEC(@sqlStr))

But it complained about invalid syntax near the word EXEC, so I am guessing dynamic queries are not allowed in the WHERE clause of a SQL statement?


Also, I guess I forgot to mention above, but the number of rows to delete needs to be a variable. I used 3 as an example above, but this would actually be a passed in variable as I used in this example.
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9598924
DELETE FROM (SELECT TOP 3 FROM tblName)

Another example
DELETE authors
FROM (SELECT TOP 10 * FROM authors) AS t1
WHERE authors.au_id = t1.au_id

HTH

Namasi Navaretnam
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9598938
DELETE Table1 t1
From (SELECT TOP 3 FROM Table1) as t2
WHERE t1.pk_id = t2.pk_id

HTH
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9599327
>>Also, I guess I forgot to mention above, but the number of rows to delete needs to be a variable.<<
Then use ROWCOUNT.  TOP requires it to be a constant.  You could use Dynamic SQL, but then you would be opening yourself up to lousy security and bad performance.

Anthony
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:Jeffbub213
ID: 9602660
Anthony-

Thats what I was planning on doing, but do you have any idea on why Books on Line suggests you not use the SET ROWCOUNT method??

Also just out of my own curiosity, there is no DELETE TOP 3 FROM tbl command right? I assume what the books on line meant was using TOP in the subquery, LIKE

DELETE FROM tbl WHERE fld IN (SELECT TOP 3 FROM tbl)

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
ID: 9602889
>>but do you have any idea on why Books on Line suggests you not use the SET ROWCOUNT method??<<
The Select TOP clause was introduced with SQL 7 (as an aside, it had been in MS Access long before that), prior to that the only way to limit the rows was by using SET ROWCOUNT.  Obviously it is more efficient for queries to use TOP, providing that that value is constant, hence the recomendation.  But this still does not address the case when you are doing a Delete or when the value is variable.  For this you must use SET ROWCOUNT or resort to Dynamic SQL

>>there is no DELETE TOP 3 FROM tbl command right?<<
Take a look at the definition of DELETE in BOL you will see that there is no support for the TOP clause.

I hope this helps,
Anthony
0
 
LVL 15

Assisted Solution

by:namasi_navaretnam
namasi_navaretnam earned 100 total points
ID: 9603110
Try This,

SET @sqlStr = ' DELETE FROM tblName WHERE field1 IN ( SELECT TOP ' + CONVERT(varchar(2), @numRows) + ' field1 FROM tblName )'
EXEC(@sqlStr)

TOP is not supported with DELETE.

HTH,

Namasi Navaretnam
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9603170
namasi_navaretnam,

Exactly, but then you are having to "resort to Dynamic SQL" and "opening yourself up to lousy security and bad performance" as I mentioned before.

Anthony
0
 

Author Comment

by:Jeffbub213
ID: 9604011
Thanks both Anthony and Namasi for your comments. I split the points between the two of you (and increased them a bit too). Since Anthony gave the answer I was looking for first, I gave him more of the points. But the method suggested by Namasi is a solution to the problem I posted. Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 34 81
SQL Server - Slabs 9 37
Find data in a column which is not in  a date format 29 10
sql server group by every 2hrs 5 7
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

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

22 Experts available now in Live!

Get 1:1 Help Now