我正在尝试记录对数据库的所有更改,但无法找到将当前用户名获取到触发器的方法。我有triggerData
一个存储用户信息的表格,
(guid (userid), data (username), logintime)
这些信息是在用户登录时插入的。
这是触发器
declare @UserIsOnlineTimeWindow DateTime
declare @currenttime DateTime
set @currenttime = GETDATE()
set @UserIsOnlineTimeWindow = 10
DECLARE @uname nvarchar(max)
**set @uname = (SELECT T.UserName from (SELECT distinct u.UserName FROM aspnet_Users u
WHERE IsAnonymous = 'FALSE' AND ((@currenttime - @UserIsOnlineTimeWindow) < u.LastActivityDate)) as t,
TriggerData td, aspnet_Users au
WHERE t.UserName = td.Data and td.guid = au.UserId and td.logintime = (select MAX(td.logintime) from TriggerData td))**
DECLARE @ComputerName nvarchar(50)
DECLARE @IPAddr nvarchar(50)
DECLARE @BroadcastIP nvarchar(50)
DECLARE @auditInsert nvarchar(255)
SET @ComputerName = (SELECT ComputerName FROM inserted)
SET @IPAddr = (SELECT ISNULL(IPAddr,0) FROM inserted)
SET @BroadcastIP = (SELECT ISNULL(BroadcastIP,0) FROM inserted)
SET @auditInsert = @ComputerName+' '+@IPAddr+' '+@BroadcastIP
Begin
INSERT INTO Audit(OldInfo ,NewInfo,[User],Date1,Type,TableName) VALUES('New Record',@auditInsert, @uname ,GETDATE(),'Added','LabIP')
End
对用户名的查询不会给出当前正在修改的用户,而是给出最近登录的用户。任何帮助表示赞赏。谢谢!