views:

246

answers:

1

For the purpose of organizing streamlined database timer-based polling, with every updating of database table I need to update a timestamp field in a SQL Server 2008 database (then poll, read its value, compare with the value in front-end and making decisions about reading the whole table).

Regarding all this, how I could read system time for writing timestamp? What data format of SQL Server field to use (DataTime or Time)? And how values of this format could be compared?

+2  A: 

Are you talking about a database column of TIMESTAMP type??

Those are binary timestamp that SQL Server will update internally - they have nothing to do with date and/or time - it's just a binary-encoded counter, really. These fields are handled by the system, and they're reliable and very accurate - if you want to have some kind of an optimistic concurrency locking mechanism, use this field type.

If you want a DATETIME field containing a date and time of the last modification, you're best option would be to have a AFTER INSERT/AFTER UPDATE trigger on that table which sets the last updated column to the current date/time of the SQL Server automatically, without you having to do anything about it.

DATETIME in SQL Server 2005 is accurate to about 3.33ms - in SQL Server 2008, DATETIME2 can be made accurate to 100ns, if needed. As such, do not rely on a DATETIME column for 100% accurate checking whether something has changed or not! The 3.33ms accuracy might give misleading results!

marc_s
Marc, thanks for the clarification! Could you, please, tell, what c# data type to use on the wpf application side for holding and comparing TIMESTAMP type of SQL Server?
rem
@rem: the "native" type for TIMESTAMP is `byte[8]` - an array of 8 bytes. Since it's 8 bytes, you can easily convert it to `BIGINT` on the server (CAST(yourcol as BIGINT)) and then use the .NET type `long` for that.
marc_s