在mysql中,可以通过开启log-output=TABLE,general-log=1的方式来把日志写入表中,那同理,在sqlserver中是否有这样的功能呢?小弟对sqlserver没有太多的使用经验,目前为了完成这个功能,需要这方面的值,烦请各位指点一二:
如何能够以sql语句的方式去获取一段时间内对库的操作语句,我需要的是在一个时间点的操作语句就可以了。sqlserver日志
如何能够以sql语句的方式去获取一段时间内对库的操作语句,我需要的是在一个时间点的操作语句就可以了。sqlserver日志
这个,我查询了一下,没有合适的方式呀,我需要的是通过语句来获取sql操作记录
嗯,我需要的是sql操作的语句,哪种操作类型是的确可以直接通过触发器获取的,就是语句有点麻烦
触发器的话,应该也是获取不到语句的,只能获取到操作类型,假如有类似于mysql的general-log或者oracle的审计功能就好了
这个触发器只能限制或者允许某些操作,确实不能记录已经执行的SQL语句,还有一个办法是审核,通过设置服务器审核规范,再设置数据库审核规范,能够记录下当前数据库的各种操作,包括update、insert、delete、select语句,你可以试试
去百度里输入:SQL Server 2008 数据库审核
就会有一些介绍的文章,很不错,应该基本上能满足你的应用需求的。
我写了一个例子,你可以参考一下:--1.1创建服务器审核对象的操作,必须在master数据库中执行
use master
go
--1.2创建服务器审核对象
create server audit wcc_server_audit
to file
(
filepath = 'C:\', --审核日志的文件路径,或者:APPLICATION_LOG,SECURITY_LOG
maxsize = 500MB, --审核文件可以增大到的最大大小
max_rollover_files = 10, --保留在文件系统中,外加当前文件的最大文件数
reserve_disk_space = off --按MAXSIZE值为磁盘上的文件预先分配大小
)
with
(
queue_delay = 1000, --确定在强制处理审核操作之前,可以延迟的毫秒数
on_failure = continue --当不能写入目标时,sql实例是否关闭
)
--2.创建服务器审核规范,捕获实例范围的事件
create server audit specification wcc_server_audit_specification
for server audit wcc_server_audit
--add (SERVER_ROLE_MEMBER_CHANGE_GROUP),
--ADD (DBCC_GROUP),
--ADD (BACKUP_RESTORE_GROUP)
WITH (STATE = ON )
--3.创建数据库审核规范,捕获数据库范围的事件
create database test
gouse test
gocreate database audit specification wcc_database_audit_specification
for server audit wcc_server_audit
--add (DATABASE_PRINCIPAL_IMPERSONATION_GROUP), --跟踪所有模拟行为
add (insert,update,delete,select,execute
on schema::dbo by dbo)
with
(state = on )
alter database audit specification wcc_database_audit_specification
with (state = off)
alter database audit specification wcc_database_audit_specification
add (insert,
update,
delete, --包含了:delete、truncate table
select,
execute
on schema::dbo by dbo)alter database audit specification wcc_database_audit_specification
add (SCHEMA_OBJECT_CHANGE_GROUP) --有:create、drop、alter(alter、truncate table)
/*
alter database audit specification wcc_database_audit_specification
drop (insert,
update,
delete, --包含了:delete、truncate table
select,
execute
on schema::dbo by dbo)alter database audit specification wcc_database_audit_specification
drop (SCHEMA_OBJECT_CHANGE_GROUP) --有:create、drop、alter(alter、truncate table)
*/
alter database audit specification wcc_database_audit_specification
with (state = on)
go
--注意:无法从用户数据库中对服务器审核执行更改,
--此操作必须在 master 数据库中执行。
use master
go--5.开启服务器审核
alter server audit [wcc_server_audit]
with (state = on )
--6.下面这些操作都能审核
use test
go
create table tt1(v1 int,v2 datetime,v3 numeric(10,0))
insert into tt1
values(1,GETDATE(),123)
update tt1
set v3 = 100 delete from tt1
truncate table tt1 --会有2条,一条是DL:就是delete,一条是AL:就是alter
create table tttt(v int)
drop table ttt
select *
from tttt
--显示记录的信息,需要去重,因为会有重复信息
select distinct
af.event_time,
af.statement,
--af.action_id,
af.session_id,
af.server_principal_id,
af.database_principal_id,
af.target_server_principal_id,
af.target_database_principal_id,
af.session_server_principal_name,
af.server_principal_name,
af.database_principal_name,
af.target_server_principal_name,
af.server_instance_name,
af.database_name,
af.schema_name,
af.object_name,
af.object_id,
af.class_type,
af.file_name,
af.audit_file_offset
from fn_get_audit_file('C:\wcc_server_audit_*',
default,
default) af
--inner join sys.dm_audit_actions aa
-- on aa.action_id = af.action_id
-- and aa.class_desc = af.class_
where 1=1
--aa.name in ('INSERT','UPDATE','SELECT','DELETE')
--and aa.class_desc = N'OBJECT'
--and af.schema_name = 'dbo'
--and af.object_name = ''
order by 1 desc