Jeffbub213
asked on
DELETE TOP syntax
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.
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.
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
Another example
DELETE authors
FROM (SELECT TOP 10 * FROM authors) AS t1
WHERE authors.au_id = t1.au_id
HTH
Namasi Navaretnam
DELETE Table1 t1
From (SELECT TOP 3 FROM Table1) as t2
WHERE t1.pk_id = t2.pk_id
HTH
From (SELECT TOP 3 FROM Table1) as t2
WHERE t1.pk_id = t2.pk_id
HTH
>>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
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
ASKER
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)
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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.
ASKER
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.