22,300
社区成员




时间磋可以转换成 二进制数据流来做
--时间戳类型和bigint互相转化示例:byjinjazz
setnocounton
--申明3个时间戳
declare@timeFlag1bigint
declare@timeFlag2bigint
declare@timeFlag3bigint
--建立表,timestamp类型不需要字段名
createtabletest(timestamp,aint)
--插入1记录时间戳,@@dbts为数据库时间戳
insertintotestselectnull,1
set@timeFlag1=cast(@@dbtsasbigint)
--插入2记录时间戳
insertintotestselectnull,2
set@timeFlag2=cast(@@dbtsasbigint)
--更新3记录时间戳
updatetestseta=3wherea=2
set@timeFlag3=cast(@@dbtsasbigint)
--时间戳1的记录
select*fromtestwheretimestamp=cast(@timeFlag1asvarbinary(8))
--时间戳2的记录已经不存在了
select*fromtestwheretimestamp=cast(@timeFlag2asvarbinary(8))
--时间戳3的记录
select*fromtestwheretimestamp=cast(@timeFlag3asvarbinary(8))
--删除表
droptabletest
setnocountoff
/**//*--测试结果
timestamp a
-----------------------------
0x000000000000B5531
timestamp a
-----------------------------
timestamp a
-----------------------------
0x000000000000B5553
*/
timestamp 通常用作给表行加版本戳的机制。 存储大小为 8 个字节。 timestamp 数据类型只是递增的数字,不保留日期或时间
每个数据库都有一个计数器,当对数据库中包含 timestamp 列的表执行插入或更新操作时,该计数器值就会增加。 该计数器是数据库时间戳。 这可以跟踪数据库内的相对时间,而不是时钟相关联的实际时间。 一个表只能有一个 timestamp 列。 每次修改或插入包含 timestamp 列的行时,就会在 timestamp 列中插入增量数据库时间戳值。使用某一行中的 timestamp 列可以很容易地确定该行中的任何值自上次读取以后是否发生了更改。 如果对行进行了更改,就会更新该时间戳值。 如果没有对行进行更改,则该时间戳值将与以前读取该行时的时间戳值一致。
你如果是要返回数据库的当前时间戳值,可以使用 @@DBTS。
@@DBTS 返回当前数据库最后使用的时间戳值。 插入或更新包含 timestamp 列的行时,将产生一个新的时间戳值。
USE AdventureWorks;
GO
SELECT @@DBTS
-------------------------------------------------
这样是吗?
SELECT
CAST(GETDATE() AS timestamp) AS UsingCast,
CONVERT(timestamp, GETDATE(), 120) AS UsingConvert;
GO
这是取当前时间并转换成 timestamp