SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Question of the Day for 08 Nov 2005


Question of the Day for 08 Nov 2005

Author
Message
Site Owners
Site Owners
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: Administrators
Points: 12493 Visits: 14
Comments posted to this topic are about the Question of the Day for 08 Nov 2005 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=639.
Yelena Varshal
Yelena Varshal
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8486 Visits: 600

I am always cautious to delete anything from the top. You delete the first line, the second line becomes the first line etc.

Addionally, why did the question say "remove from a data load" ? When you are removing from a data load, it is being removed from the source file during the process of loading. In this case it is just deleting from a regular table.




Regards,
Yelena Varshal

galaxis
galaxis
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 5

Only in SS2K5 can you use the TOP keyword with DELETE. Wouldn't this work for any version (and still use TOP)?:

DELETE customers FROM (SELECT TOP 10 * FROM customers ORDER BY DateAdd DESC)




Vic Kirkpatrick-173212
Vic Kirkpatrick-173212
Right there with Babe
Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)Right there with Babe (751 reputation)

Group: General Forum Members
Points: 751 Visits: 76
I get a syntax error in 2005 with any of these 4 commands. The "correct" one gives this error:

Incorrect syntax near the keyword 'order'.

I don't think you can have an order by clause with a delete statement, even in 2005. Am I missing something?


Adam Machanic
Adam Machanic
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2879 Visits: 714
I believe you are correct.

The only way to do this is:

DELETE Customer
WHERE CustomerId IN
(
SELECT TOP 10 CustomerId
FROM Customer
ORDER BY DateAdded DESC
)

I'm not sure what DELETE FROM (SELECT TOP 10) does, but I wouldn't trust it without correlation, personally.

--
Adam Machanic
SQL Server MVP
SQLblog.com: THE SQL Server Blog Spot on the Web
Adam Machanic
Adam Machanic
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2879 Visits: 714
Ahhh, answered my own question:

--
DECLARE @tbl table (id int)
insert @tbl values (1)
insert @tbl values (2)
insert @tbl values (3)

delete x from (select top 1 id from @tbl order by id desc) x

select * from @tbl
--

Result:

Server: Msg 4428, Level 16, State 1, Line 6
The derived table 'x' is not updatable because the definition contains the TOP clause.

--
Adam Machanic
SQL Server MVP
SQLblog.com: THE SQL Server Blog Spot on the Web
RichB
RichB
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2896 Visits: 1065
so, another top notch question then?



Andrej Ruckij
Andrej Ruckij
SSC Veteran
SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)

Group: General Forum Members
Points: 289 Visits: 1
Hi,

can you tell me why this is incorrect ?

set rowcount 10 delete customers order by DateAdded desc set rowcount 0
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5112 Visits: 2767
nice and easy question to grab +2 points :-P

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search