我是SQL新手。 我正在使用SQL Server 2008。 我想在最近1分钟内从表中添加记录。 实际上我每隔1分60秒就会调用一次SP,并且我没有任何保存修改日期的列,所以我必须选择在时间间隔内添加的所有行。
可能不会在最后一分钟内添加任何行或添加N行,因此我需要在1分钟的计时器间隔内添加所有行,因为SP将在每1分钟后被触发。
答案 0 :(得分:1)
要获取最后一分钟插入的表中的所有记录,您需要在表上具有DEFAULT GETDATE()约束的日期字段,或者在插入记录时手动插入日期。 / p>
SELECT *
FROM MyTable
WHERE MyDateColumn >= DATEADD(mi, -1, GETDATE())
在现有表格上创建一个默认列:
ALTER TABLE MyTable
ADD MyDateColumn DATETIME2 NOT NULL DEFAULT GETDATE()
您可以从sys或INFORMATION_SCHEMA模式中获取表/行元数据,但我真的不推荐它。
说,也许您的应用程序/数据库适合SQL Server审核?
http://technet.microsoft.com/en-us/library/cc280386.aspx
<强>更新强>
这并不能完全回答您的问题,但它可能是一个可行的解决方案。我写了一个游标来创建并将审计触发器应用于数据库中的所有表。设置触发器以记录到单独的表(当前是硬编码的)。您可以根据需要对其进行修改,但我已在下面提供了所有相关代码。我想你需要删除UserId及其约束:
-- Stores the types of audit entries for the "Logs" table, such as "RECORD CREATED", "RECORD MODIFIED", "RECORD DELETED" and other, miscellaneous log types
CREATE TABLE dbo.LogTypes (
LogTypeID INT NOT NULL IDENTITY PRIMARY KEY,
[Description] VARCHAR(100) NOT NULL UNIQUE
)
CREATE TABLE dbo.[Logs] (
LogID INT NOT NULL IDENTITY PRIMARY KEY,
LogTypeID INT NOT NULL FOREIGN KEY REFERENCES LogTypes(LogTypeID),
UserID INT NOT NULL FOREIGN KEY REFERENCES Users(UserID), -- User that created this log entry.
SysObjectID INT, -- The ID of the table in sysobjects (if this is a CRUD insert from a trigger)
Details VARCHAR(1000),
DateCreated DATETIME NOT NULL DEFAULT GETDATE()
)
-----------------------------------------------------------------------------
-- Log types
-----------------------------------------------------------------------------
SET IDENTITY_INSERT LogTypes ON
INSERT INTO LogTypes (LogTypeID, [Description])
VALUES(1, 'Record Insert')
INSERT INTO LogTypes (LogTypeID, [Description])
VALUES(2, 'Record Update')
INSERT INTO LogTypes (LogTypeID, [Description])
VALUES(3, 'Record Deletion')
INSERT INTO LogTypes (LogTypeID, [Description])
VALUES(4, 'User logged in')
INSERT INTO LogTypes (LogTypeID, [Description])
VALUES(5, 'User logged out')
SET IDENTITY_INSERT LogTypes OFF
光标代码(运行一次):
DECLARE @table_name VARCHAR(500), @instruction VARCHAR(MAX)
DECLARE curTables CURSOR READ_ONLY FAST_FORWARD FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME <> 'Logs'
OPEN curTables
FETCH NEXT FROM curTables INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
-- Drop any existing trigger
SET @instruction = 'IF OBJECT_ID (''tr_' + @table_name + '_Audit'',''TR'') IS NOT NULL DROP TRIGGER tr_' + @table_name + '_Audit;'
exec sp_sqlexec @instruction
-- Create the new trigger
SET @instruction = 'CREATE TRIGGER tr_' + @table_name + '_Audit
ON ' + @table_name + '
AFTER INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON
DECLARE @LogTypeID INT, @SystemUserID INT, @SysObjectID INT, @TableName VARCHAR(500)
SET @SystemUserID = 1 -- System account
SET @TableName = ''' + @table_name + '''
IF EXISTS(SELECT * FROM Inserted) AND EXISTS(SELECT * FROM Deleted)
SET @LogTypeID = 2 -- Update
ELSE IF EXISTS(SELECT * FROM Inserted)
SET @LogTypeID = 1 -- Insertion
ELSE IF EXISTS(SELECT * FROM Deleted)
SET @LogTypeID = 3 -- Deletion
SET @LogTypeID = ISNULL(@LogTypeID, 0)
IF @LogTypeID > 0
BEGIN
-- Only log if successful
SELECT
@SysObjectID = id
FROM sysobjects (nolock)
where [name] = @TableName
AND [type] = ''U''
INSERT INTO [Logs] (LogTypeID, UserID, SysObjectID, Details, DateCreated)
VALUES(@LogTypeID, @SystemUserID, @SysObjectID, NULL, GETDATE())
END'
exec sp_sqlexec @instruction
FETCH NEXT FROM curTables INTO @table_name
END
CLOSE curTables
DEALLOCATE curTables
数据库中的每个表现在都将所有INSERT,UPDATE和DELETE记录到Log表中。但是,请注意,向表中添加触发器会增加I / O和内存使用量,从而降低性能。不过,对你来说这可能不是一个大问题。