I need help in ordering my resultset of SQL Server 2005. My Query is
Select RQuery from EPMaster where PName IN ('PFNAME','POName','PFSName');
The result shown is in order as stored in the table EPMaster
In the table EPMaster, the order for values of PName are:
PFName -> 1
PFSName -> 2
POName -> 3
1 being the topmost and 3 being lowest
I need the results in order of the passed parameters of the query made with IN. It means that the result should be in the form
PFName -> 1
POName -> 2
PFsName -> 3
Selectdoes not guarantee any sort of order unless you specify anorder byclause.For your particular case, you can use something like:
but you should be aware that this sort of query may be a performance killer.
If your query is created dynamically, and you have no control over the order in which the possibilities are given, yet still want to dictate that the rows are in the same order, you may have to resort to building your query differently, such as with:
If your code builds this query rather than the
invesrsion, you can guarantee that the rows will be returned in the order as specified, because of thexyzzycolumn you add (and sort by).