Click here to Skip to main content
15,066,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table with 3 fields ID, Name and Roll Number

lets suppose it as, 1 Santhosh 1
2 Govind 2
3 Nikhil 3

My Question is, When I Insert a new Data,say Divya with Roll Number 4, i want to sort my table on the ALPHABETICAL ORDER of Names. That is, my output should be as follows..

Name Roll Number

Divya 1
Govind 2
Nikhil 3
Santhosh 4

How can i do this in sql server 2008??? Can anybody help me please????
Thanks,
Rahul..
Posted
Comments
Osman24 23-May-13 3:27am
   
Why do not you just insert and use your query with order by Name asc
Mahesh Bailwal 23-May-13 3:30am
   
You can use SQL server "Order by Name" while you select data from table. Are you facing any problem using "order by" or requirement is something else?
Raahul Satheesh 23-May-13 6:16am
   
but what i need is that i want to sort my entire table in which the new entry is having the roll number based on the alphabetical order of name
Raahul Satheesh 23-May-13 6:13am
   
but i think that its possible with trigger

I DON'T think so. its not possible while insert
but you can do while select the above record you can do it like this

SQL
create table Satheesh(fieldid int identity(1,1), names varchar(20), rollno int)



insert into Satheesh values('Santhosh',1)
insert into Satheesh values('Govind',2)
insert into Satheesh values('Nikhil',3)
insert into Satheesh values('divya',4)

select row_number() over (order by names) as rollno, names  from satheesh
order by names



result

VB
rollno               names
-------------------- --------------------
1                    divya
2                    Govind
3                    Nikhil
4                    Santhosh

(4 row(s) affected)
   
Comments
Raahul Satheesh 23-May-13 6:12am
   
but this is not i what i want
Any way thanks for your information
As per my understanding in SQL server table data is stored in B-Tree data structure which is based on your clustered index. So record by default is ordered by your clustered index column. In this case you may need change your clustered index and check the result.
   

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