SQL Server日志记录/用户跟踪功能

时间:2009-03-31 20:34:24

标签: sql-server sql-server-2005 sql-server-2008 logging tracking

我们最近转换了一个Access Application来将日期存储在SQL Server上。表单仍驻留在MS Access中,但通过文件DSN链接到SQL服务器。

在SQL服务器中跟踪用户活动的能力出现了一个问题。

我推测,如果我们在SQL服务器中设置个人用户并在用户计算机上设置DSN时使用这些个人帐户,那么我们可能会使用SQL服务器来跟踪用户活动。这是真的吗?

我们目前有SQL Server 2005标准,但将很快升级到2008年。

感谢您的任何建议!

3 个答案:

答案 0 :(得分:1)

升级到SQL 2008时,您有Change Data Capture

答案 1 :(得分:1)

我目前正在跟踪DDL更改,因此这里有一个DDL触发器,带有所需的2个函数和表结构...在运行代码集之前,为数据库设置ext道具“DbVersion”(例如2.3.4)和“ DbType“(例如dev,test,prod)并将[ga]命名空间替换为您选择的命名空间或创建一个... 编辑:修复bug与null版本,如果对象是新的...

USE [GA_DEV]
GO

/****** Object:  DdlTrigger [TraceDbChanges]    Script Date: 05/07/2009 11:15:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


create trigger [TraceDbChanges]
on database
for create_procedure, alter_procedure, drop_procedure,
create_table, alter_table, drop_table,
create_function, alter_function, drop_function , 
create_trigger , alter_trigger , drop_trigger 
as

set nocount on

declare @data xml
set @data = EVENTDATA()
declare @DbVersion varchar(20)
set @DbVersion =(select ga.GetDbVersion())
declare @DbType varchar(20)
set @DbType =(select ga.GetDbType())
declare @DbName varchar(256)
set @DbName =@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)')
declare @EventType varchar(256)
set @EventType =@data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(50)')
declare @ObjectName varchar(256)
set @ObjectName  = @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)')
declare @ObjectType varchar(25)
set @ObjectType = @data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)')
declare @TSQLCommand varchar(max)
set @TSQLCommand = @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)')
declare @opentag varchar(4)
set @opentag= '<'
declare @closetag varchar(4) 
set @closetag= '>'
declare @newDataTxt varchar(max) 
set @newDataTxt= cast(@data as varchar(max))
set @newDataTxt = REPLACE ( REPLACE(@newDataTxt , @opentag , '<') , @closetag , '>')
-- print @newDataTxt
declare @newDataXml xml 
set @newDataXml = CONVERT ( xml , @newDataTxt)

declare @Version varchar(50)
set @Version = @newDataXml.value('(/EVENT_INSTANCE/TSQLCommand/CommandText/Version)[1]', 'varchar(50)')

-- if we are dropping take the version from the existing object 
if  ( SUBSTRING(@EventType , 0 , 5)) = 'DROP'
set @Version =( select top 1 [Version]  from ga.DbObjChangeLog where ObjectName=@ObjectName order by [LogId] desc)
if ( @Version is null)
set @Version = '1.0.0'

declare @Description varchar(max)
set @Description = @newDataXml.value('(/EVENT_INSTANCE/TSQLCommand/CommandText/Description)[1]', 'varchar(max)')

declare @ChangeDescription varchar(max)
set @ChangeDescription = @newDataXml.value('(/EVENT_INSTANCE/TSQLCommand/CommandText/ChangeDescription)[1]', 'varchar(max)')

declare @LoginName varchar(256) 
set @LoginName = @data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)')
declare @FirstName varchar(50)
set @FirstName= (select [FirstName] from [ga].[LoginsForUsers] where [LoginName] = @LoginName)
declare @LastName varchar(50)
set @LastName  = (select [LastName] from [ga].[LoginsForUsers] where [LoginName] = @LoginName)
declare @SchemaName sysname 
set @SchemaName = @data.value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname');
--declare @Description xml 
--set @Description = @data.query('(/EVENT_INSTANCE/TSQLCommand/text())')




print 'VERSION IS ' + @Version
print @newDataTxt
print cast(@data as varchar(max))


-- select column_name from information_schema.columns where table_name ='DbObjChangeLog'
insert into [ga].[DbObjChangeLog]
(
[DatabaseName] ,
[SchemaName],
[DbVersion] ,
[DbType],
[EventType],
[ObjectName],
[ObjectType] ,
[Version],
[Description],
[ChangeDescription],
[SqlCommand] ,
[LoginName] ,
[FirstName],
[LastName] 
)

values(

@DbName,
@SchemaName,
@DbVersion,
@DbType,
@EventType, 
@ObjectName, 
@ObjectType , 
@Version,
@Description,
@ChangeDescription,
@newDataTxt, 
@LoginName , 
@FirstName , 
@LastName
)


GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

DISABLE TRIGGER [TraceDbChanges] ON DATABASE
GO

ENABLE TRIGGER [TraceDbChanges] ON DATABASE
GO

答案 2 :(得分:0)

  

这是真的吗?

是的,用于捕获更改的表上的触发器将起作用。您还可以设置服务器端跟踪,以便在您访问该类信息后将登录事件记录到表中。更具体的一些答案。