6
I have a simple table with only one text field, no field auto increment
and indexes
CREATE TABLE teste (Nome varchar(30) NOT NULL)
I inserted the information in this table in the following order:
INSERT INTO teste(Nome) VALUES('C')
INSERT INTO teste(Nome) VALUES('D')
INSERT INTO teste(Nome) VALUES('B')
INSERT INTO teste(Nome) VALUES('A')
If I make a select sorted by name will return the sort by name, ok.
select * from teste order by nome - resultado: A,B,C,D
What I need to do is a select that returns to me the order that was entered, i.e., that the expected result is C,D,B,A.
Is it possible to get this result in a simple table like this?
Is there a hidden index that can do this order by
?
I think the best way would be to create a field with
auto increment
and order by that field– Tales Peres
Are you DBA? if yes, put an auto increment field, it will be better for your bank’s performance than doing a magic with order by
– Luiz Augusto
A table without a primary key is a bad idea, even if you don’t make "direct" use of it. Create a field with a numeric key and Identity and sort by it, as suggested by @Sorack, without that information you will not know the order they were entered. Still I suggest you have a date field, with default value
getdate()
. The seed of Identity can be changed, which will mess up your search if it happens– Ricardo Pontual
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack