Microsoft SQL Server: Any way to tell when a record was created?

后端 未结 5 1814
暖寄归人
暖寄归人 2021-01-11 15:14

Our customer wants to order by the record creation date. To me this seems like a system variable, some sort of meta data for the record itself.

Is there a way to te

相关标签:
5条回答
  • 2021-01-11 15:53

    If he only wants to order by it, you can consider using a timestamp column.

    It cannot be related to a real-world date, but you can sort on it.

    0 讨论(0)
  • 2021-01-11 15:56

    Nope.

    You need to have a column for this.

    Imagine how big the meta-data would be if you needed to keep a record for each record for creation! Would you also want to keep meta-data on your meta-data so you know when the meta-data was updated? The space use can quickly escalate.

    SQL Server keeps some stats but something this specific will need to come from a user-defined field.

    As a side note, you can make it more difficult to tamper with the date on your created field if you use a lookup table. Create a table "TableName_CreateDate" and use the PK from your actual table and a date value. Your date is in a separate location and less likely to be modified but you can still JOIN on it to get your order. You would need to create a trigger to update this with new values.

    If you only want the DATE and don't need a datetime value, you can go one step further and just have a table of dates and a lookup table that joins to that. I.e.:

    Table->Table.PK + Date.Pk -> DateTable

    This would save a lot of drive space if you have a lot of rows (4 bytes per row I think).

    0 讨论(0)
  • 2021-01-11 16:00

    I once came with a kind of situation where order of the rows of a table is required but no timestamp (CreateDateTime, UpdateDateTime, ..., etc.) column is available. So, only data I had was the transaction log. Reading transaction log directly was not helpful as it is not easy to parse. Hence, I found a program called "ApexSQL Log" which was very helpful. It has all filtering options and you can read all log data without hardcore operations. Although this solution was satisfactory for me, it has a drawback: You can only filter date (Begin Time and End Time) according to transaction operation. If you are searching for a query operation, for example an insert, which is encapsulated in a transaction, you can not retrieve the exact insert time (at least I could not find a way), only the transaction time. If your operations are all atomic, then you can literaly have all row create date information.

    0 讨论(0)
  • 2021-01-11 16:03

    No, not even in the system tables to my knowledge. You could dig through old transaction logs I'm sure, but natively, no.

    0 讨论(0)
  • 2021-01-11 16:10

    The creationdate field should be added. You fix the ' hope that no one modifies it" part by adding a trigger on update that does not allow that field to be updated, ever.

    0 讨论(0)
提交回复
热议问题