Stevebin.net :: Stephen Viswaraj
Search
 
About me
Articles
Photo Gallery
fun
My College City
   MS-SQL Server 2005 / 2000 Articles  Feedback 
Changes on TOP Clause in SQL Server 2005

In SQL Server 2000, we can able use the Top Clause only with Numeric Constants. That is, it does not allow entering the variables.
SQL 2005 Top Clause allows us to do with the Variable that is very much useful.
a) User can specify an expression as an input to the TOP keyword.
b) User can use TOP in modification statements (INSERT, UPDATE, and DELETE).
            DECLARE @Rows INT
                    SET @Rows = 10
SELECT TOP (@Rows) * FROM demo.Orders
Inside the TOP Clause, you can use the Query also, which returns a numeric.
SELECT TOP(SELECT COUNT(*) FROM Sales.Customer) * FROM demo.Orders
Top Keyword provides the option to insert / update / delete only 'n' records even the select set more than Values.
//-- Order Qty updates will apply for only five records in the table "tblStevePurchase".
SQL Server 2000:
SET ROWCOUNT 5 UPDATE demo.tbStevePurchase SET OrderQty = 10 SQL Server 2005: UPDATE TOP(5) demo.tblStevePurchase SET OrderQty = 10 INSERT TOP(5) demo.tblStevePurchase SELECT LineNumber, OrderQty FROM Purchasing.PurchaseOrderDetail
//-- the below SQL will delete only two records from the table tblStevePurchase.
            DELETE TOP(2) demo.tblStevePurchase
So, Thanks to SQL 2005, to provide the TOP feature against the variables, and functions.
top
Add a Comment
Name:
Email:
Comments:

Stevebin