Sorting will not work by default in database Views. Here is a work around to sort the records in a database view.
As we all know, for sorting the values while fetching records we need to make use of ORDER BY clause.
That said, would the below code snippet work?
Create View SampleView As Select Sno, Firstname from employeeTable ORDER BY Firstname
Nope it won't!!!
The work around is, we need to use "TOP 100 PERCENT" in our query if we want to use ORDER BY clause in a View (or) a Subquery (or) a derived table. (BTW this is possible from SQL Server 7+.).
See below the modified code snippet:
Create View SampleView As Select TOP 100 PERCENT Sno, Firstname employeeTable ORDER BY Firstname
|
| Author: Kapil Dhawan 17 Jun 2008 | Member Level: Gold Points : 2 |
Hello Nice piece of code Thanks for sharing your knowledge with us. I hope to see more good code from your side This code will help lots of guys Thanks to you Regards, Kapil
|