Click here to Skip to main content
15,562,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The data of table tblLeave is as below: (note: leaveId is Primary, and leave is in index for unique data)

leaveId leave
1 Home Leave
2 Sick Leave
3 Maternity Leave
4 Maternity Leave (Male)
5 Mourning Leave

Actually I want the result as below data that leaveId should be in ascending.

XML
0   <Select All>
1   Home Leave
2   Sick Leave
3   Maternity Leave
4   Maternity Leave (Male)
5   Mourning Leave


But I Could not make it ascending.
My Code as below:
SQL
CREATE VIEW vLeave
SELECT TOP 100 PERCENT leaveId, leaveName FROM tblLeave
UNION ALL
SELECT 0,'<Select All>'
 order by leaveId


I used in this way also..
CREATE VIEW vLeave AS
SELECT 0,'<Select All>'
UNION ALL
SELECT TOP 100 PERCENT leaveId, leaveName FROM tblLeave ORDER BY leaveId

but
when I run query Like this
"select * from vLeave", it doesnot work as I want.
Posted
Updated 27-Oct-15 1:35am
v2
Comments
Maciej Los 25-Oct-15 5:22am    
Solution for what?
OriginalGriff 25-Oct-15 5:27am    
2 Things:
1) This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

2) Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know. And that includes using your email as your user name!
sulomila 27-Oct-15 6:37am    
Thank you for suggestion. And Sorry for incomplete question.

1 solution

Since SQL 2005 the ORDER BY part of a view has a different meaning in SQL (or to be most precise it optimized by different rules)...
It does not order the outcome of a view but the inner select statement only...So if you have that top statement, order by will ensure that you get the top (100% ?!?!?) part of an ordered list, but it will not affect the outcome of the view...
(I do understand that you added that TOP to enable ORDER BY, but it still will not work)...
What you have to understand that view is NOT a temporary table - the data of a view not stored anywhere...So can not be order of that data...
You have two options:
1. do the order by on the view and not inside it...
2. create a table-valued function to return the ordered values...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900