Anatoly Lubarsky

Logo
MSSQL, .NET, Design. Life and Music

MSSQL 2005 Tip: Using TOP with Subquery

SQL Server 2005 introduced new TOP behavior - like TOP function supports variables and expressions.


As a result it is possible to use a subquery as input to TOP. Subquery should return scalar. Like so:


SELECT TOP(SELECT COUNT(*)
             FROM Table2) Col1, Col2
  FROM Table1
 ORDER BY Col1

Which seems kinda strange for people with MSSQL 2000 experience. In addition it is possible to use TOP with INSERT, UPDATE and DELETE like:


DELETE TOP(1000)
  FROM Table1
 WHERE Id < 10000

Just wanted to share these small tips :)


Related Posts:

Sunday, August 05, 2007 10:53 PM

Comments

# Interesting Finds: August 6, 2007

8/6/2007 7:31 AM by Jason Haley

# re: MSSQL 2005 Tip: Using TOP with Subquery
Excellent reminders! I knew that but never used it. Thx.

8/7/2007 12:03 AM by David

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)