In MSSQL there are a little more than a dozen tables, the last 3 entries of each of which I would like to see. The first three I choose by the query:

SELECT TOP 3 * FROM table_name; 

When one of the fields contains an identifier-increment, you can sort the data by descending identifier and, again, make a SELECT TOP query ...

However, not all tables have this very increment. There is, for example, a table with only two fields: [name] and [type].

Is there any option to see the latest entries in this case?

UPD. I’m disappointed that for me it’s still a mystery how MSSQL stores data. I know that the data was written to the tables sequentially and I assume that the SELECT TOP ... query selects the first (in other words, the earliest recorded ones in the database ) records. Accordingly, I am interested in the latest data recorded in the table.

    2 answers 2

    @Denis Khvorostin does not think that the order of addition is really guaranteed in M ​​$ Sql. what lucky you still does not mean anything.

    here is an emotional investigation of mssql behavior on this topic (eng)

      In general, no. One record is no better than another, so if there is no identity or at least a date or timestamp, then there is no reason to think that some of them have been added before.

      • I have at least two reasons to think. First: each time I execute a SELECT TOP query ... I get the same data. Once the query has no explicit sorting, the data is returned in the order in which they were entered into the database. In theory, this order can somehow be used. Second, I repeatedly wrote data in MySQL, followed by the addition of a primary key. Never at the same time the data is not mixed. The primary key fixed the order of entries. - Denis Khvorostin
      • Apparently your primary key was not clustered. And the nonclustered index does not move the record. Also, if your database is compressed, the order of records in a table without a cluster key is likely to change. The same will happen when restoring from backup and / or moving to another file group. - maxleo
      • In general, if you really need it, but you don’t have a lot of records, you can do it through xml by forming a set of elements for all records and selecting those that satisfy the condition [position ()> last () - 3] (if there is a key, then shove it in xml, and then use it to get the rest of the data). But this will simply return three of some records that are the last with some probability. - maxleo