gpt4 book ai didi

sql-server - 如何在 Sql Server 2005 上获取上次插入/更新/删除日期时间?

转载 作者:行者123 更新时间:2023-12-03 15:52:41 26 4
gpt4 key购买 nike

不是 my previous question 的副本

当表/数据库在 Sql Server 2005 上插入/更新/删除时,有什么方法可以获得最新的日期时间吗?最好不要创建触发器..

我知道当您需要每行的最后一次更新时,您需要触发器。但是我不确定当您只想获取整个表的最后更新时是否需要它们。

最佳答案

您可以轻松获取上次插入/更新/删除的日期,如下所示:

CREATE FUNCTIOn fn_TablesLastUpdateDate(@Date NVARCHAR(20))

RETURNS @table TABLE(TableName NVARCHAR(40), LastUpdated Datetime)

AS

BEGIN


IF(@Date='') OR (@Date Is Null) OR (@Date='0')

BEGIN
INSERT INTO @table
SELECT TOP 100 PERCENT TABLENAME,LASTUPDATED FROM
(
SELECT B.NAME AS 'TABLENAME', MAX(STATS_DATE (ID,INDID)) AS LASTUPDATED
FROM SYS.SYSINDEXES AS A
INNER JOIN SYS.OBJECTS AS B ON A.ID = B.OBJECT_ID
WHERE B.TYPE = 'U' AND STATS_DATE (ID,INDID) IS NOT NULL
GROUP BY B.NAME
) AS A
ORDER BY LASTUPDATED DESC
END
ELSE

BEGIN
INSERT INTO @table
SELECT TOP 100 PERCENT TABLENAME,LASTUPDATED FROM
(
SELECT B.NAME AS 'TABLENAME', MAX(STATS_DATE (ID,INDID)) AS LASTUPDATED,
CONVERT(VARCHAR, MAX(STATS_DATE (ID,INDID)), 103) as Date
FROM SYS.SYSINDEXES AS A
INNER JOIN SYS.OBJECTS AS B ON A.ID = B.OBJECT_ID
WHERE B.TYPE = 'U' AND STATS_DATE (ID,INDID) IS NOT NULL
GROUP BY B.NAME
) AS A
WHERE Date=@Date
ORDER BY LASTUPDATED DESC
END
RETURN

END



-- SELECT * from fn_TablesLastUpdateDate('06/11/2012')

关于sql-server - 如何在 Sql Server 2005 上获取上次插入/更新/删除日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1227707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com