TOP Enhancements in Yukon
In previous versions of SQL Server TOP allowed you to limit the number of rows returned as a number or percentage in Select Queries. I use this all the time. It has gotten more flexible in Yukon where TOP can be used in Delete, Update and Insert queries. What is even cooler is that Yukon will allow you to specify the number of rows (or percent) by using variables or subqueries. I love the ability to dynamically set the TOP, so much that I have started writing Stored Procedures that accept a NumberofRows parameter like so:
Create Procedure usp_SEL_ReturnTopOrders
Select TOP (@NumberofRows) OrderID
Order By OrderID
Executing the stored procedure is easy, just pass in how many records that you want (in this case it is 100):
Using a subquery can be very powerful when you are doing things on the fly. A real simple example to demonstrate the concept is show here, we are getting the TOP n customers based on how many records we have in our Employees table:
Select TOP (Select Count(*) from Employees) *
See my article on SQLJunkies.com.
Page rendered at Tuesday, February 25, 2020 2:21:34 AM (Eastern Standard Time, UTC-05:00)
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in anyway.