MSSQL 2008 - Moving rows up or down without 'ORDER BY' code?

When I do a simple SELECT * FROM Mytable, it list the values in the order they were inserted, however, is there anyway to physically make a change to that order so that every time I do a select * from Mytable the changed order appears?

The reason why I need it this way is because I'm planning in inserting a 'move up' and 'move down' arrow in my grid software, and I thought this would be best instead of using ini files and complex coding.
John86aAsked:
Who is Participating?
 
itsdivyaksCommented:
I created a table with p (col1,col2,col3)
col3 - IDENTITY
and inserted some rows and using select * from p and it gave the following:
col1 col2 col3
7      2      1
7      2      2
7      2      3
1      2      4
2      2      5

Then I created index


/****** Object:  Index [test]    Script Date: 04/08/2011 16:31:03 ******/
CREATE CLUSTERED INDEX [test] ON [dbo].[p]
(
      [col1] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


After that, I run the query select * from p and then i get the following records:
col1 col2 col3
1      2      4
2      2      5
7      2      1
7      2      2
7      2      3
0
 
itsdivyaksCommented:
Another way to change the order of items in the select statement is to create a clustered index on the column you want the default select statement with out using the order by clause
0
 
John86aAuthor Commented:
Can you illustrate with an example please?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
SharathData EngineerCommented:
The records in a table are unorderd set. There is no ordering between the records until you mention the order in your query. Creating clustered index will consume some memory. Create the clustered index if you have other reason also. I dont think creating a clustered index is good idea for just sort purpose. For that matter, you have ORDER BY clause.
0
 
John86aAuthor Commented:
That's almost what I need, but it'll do.
Thank you!
0
 
Anthony PerkinsCommented:
If you are under the false illusion that SQL Server will always sort your results without using an ORDER BY clause then you had better think again: SQL Server does not guarantee any order unless an ORDER BY clause is used.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.