开发者

select records from table in the order in which i inserted

consider a tale is as follows,

EmployeeId | Name | Phone_Number

Now, i insert 10 records... When i query them back, select * from myTable they are not selected in the order i inserted. I can obviously keep an aut开发者_如何学Gooincrement index and ORDER BY index. But i dont want to alter the table. How can i do this without altering the table?


Any ordering of result must be done using ORDER BY, if you don't use it the result will be returned in an undetermined order.


Unfortunately there is no way to do this.


Without an ORDER BY clause, there is no guaranteed order for the data to be returned in.

You would need to order by a column that indicates the inserted order, such as an IDENTITY field or a "Creation Date" field.


Isn't "EmployeeId" an auto-increment field? If it is, you can order by it to get data in order in which you inserted it.


There is no standard way to do this without adding an additional date, autoincrement index or some other counter to your table. Depending on your database there are some hacks you could do with SQL triggers to track this info in a separate table, but I suspect you don't want to do that (not all databases support them and they are not generally portable).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜