Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Two more tools for your bag of T-SQL tricks

gwilliams

Have you ever been writing a new view and had the need to use an ORDER BY only to be greeted by an error message when you go to create the view. Never fear, there is a solution. ORDER BY can only be used in views, inline functions, derived tables, sub-queries, and common table expressions if the keywords TOP or FOR XML are used. Thus we can either use TOP or FOR XML to get our solution. We will on talk about the TOP Keyword today and save FOR XML for another time.

TOP allows you to select the first ā€˜nā€™ records returned from a select. Consider the following table of s