`
zhangziyangup
  • 浏览: 1186439 次
文章分类
社区版块
存档分类
最新评论

Schema Changes History数据从哪里来?

 
阅读更多

SQL Server提供了Schema Changes History report可以用来追踪DDL相关信息。 但是Schema Changes History report的数据是从哪里来的呢?

首先我启动SQL Profiler trace然后打开Schema Changes History report(Management studio->Report->Standard report->Schema Changes History report)

从Profiler trace我看到下面的SQL 语句:


select @curr_tracefilename = path from sys.traces where is_default = 1 ;
set @curr_tracefilename = reverse(@curr_tracefilename)
select @indx = PATINDEX(''%\%'', @curr_tracefilename)
set @curr_tracefilename = reverse(@curr_tracefilename)
set @base_tracefilename = LEFT( @curr_tracefilename,len(@curr_tracefilename) - @indx) + ''\log.trc'';

insert into @temp_trace
select ObjectName
, DatabaseName
, StartTime
, EventClass
, EventSubClass
, ObjectType
, ServerName
, LoginName
, ApplicationName
, ''temp''
from ::fn_trace_gettable( @base_tracefilename, default )
where EventClass in (46,47,164) and EventSubclass = 0 and DatabaseID <> 2

update @temp_trace set ddl_operation = ''CREATE'' where event_class = 46
update @temp_trace set ddl_operation = ''DROP'' where event_class = 47
update @temp_trace set ddl_operation = ''ALTER'' where event_class = 164

select @d1 = min(start_time) from @temp_trace
set @diff= datediff(hh,@d1,getdate())
set @diff=@diff/24;

select @diff as difference
, @d1 as date
, object_type as obj_type_desc
, *
from @temp_trace where object_type not in (21587)
order by start_time desc
end

我们看一下event_class 46,47,164代表什么:

select trace_event_id,name from sys.trace_events where trace_event_id in ('46','47','164')

trace_event_id name
-------------- --------------------------------------------------------------------------------------------------------------------------------
46 Object:Created
47 Object:Deleted
164 Object:Altered

(3 row(s) affected)

从上面的内容我们可以看到这个Report只是从Default trace中抓取了创建删除和更改三个事件,然后展现给我们。 所以如果我们Disable了Default trace file,就无法从这张Report中查询到数据。

另外如果DDL发生在当前Default trace file开始之前也是显示不出来的,但是我们可以将上面的脚本修改一下,直接用当时的Trace File就可以获得相关信息了(SQL Server 默认会保存5份Trace文件)

分享到:
评论

相关推荐

    mongoose-patch-history:Mongoose插件,可为关联的“补丁”集合中属于模式的所有文档保存JSON补丁操作的历史记录

    Mongoose补丁历史记录是一个Mongoose插件,它为关联的“补丁”集合中属于模式的所有文档保存操作的历史记录。... import mongoose , { Schema } from 'mongoose' import patchHistory from 'mongoose-patch-history' /*

    MySQL 5.5 参考手册(英文版)

    **InnoDB I/O Subsystem Changes(InnoDB I/O子系统变更)**:InnoDB存储引擎是MySQL的核心组件之一,新版对其I/O子系统进行了深度优化,包括但不限于支持更多的I/O操作模式、提高I/O效率,从而提升了整个数据库系统...

    python3.6.5参考手册 chm

    Other Language Changes New Modules secrets Improved Modules array ast asyncio binascii cmath collections concurrent.futures contextlib datetime decimal distutils email encodings enum ...

    Apache Solr [Apache Con 2006]

    #### History and Background Apache Solr has an interesting history that began with the search for a replacement search platform. Initially, the available commercial solutions came with high licensing...

    艺术品管理系统的设计与实现 .doc

    It not only streamlines operational processes but also enriches the cultural exchange and educational opportunities surrounding these invaluable pieces of human history and creativity. With careful ...

    PLSQL.Developer(X32) v12.0.1.1814主程序+ v11中文包+keygen

    Changes made and saved in an external editor will automatically be propagated to the column data. The SQL Window will now navigate to the offending cell in the result set after an insert or update ...

    plsqldev12.0.4.1826x32主程序+ v12中文包+keygen

    Changes made and saved in an external editor will automatically be propagated to the column data. The SQL Window will now navigate to the offending cell in the result set after an insert or update ...

    PLSQL.Developer(X64) v12.0.1.1814 主程序+ v11中文包+keygen

    Changes made and saved in an external editor will automatically be propagated to the column data. The SQL Window will now navigate to the offending cell in the result set after an insert or update ...

    plsqldev12.0.4.1826x64主程序+ v12中文包+keygen

    Changes made and saved in an external editor will automatically be propagated to the column data. The SQL Window will now navigate to the offending cell in the result set after an insert or update ...

    BDB Professional Edition v2.7

    1. Revision History v2.7 _________________________________________________ * Added: Intelligent code completion for fast query support(Cross-database); * Added: Identity column support; * Added: ...

    Professional C# 3rd Edition

    Persisting DataSet Changes 723 Updating with Data Adapters 724 Writing XML Output 726 Working with ADO.NET 728 Tiered Development 728 Key Generation with SQL Server 730 Naming Conventions 732 Summary ...

    MySQL 5.6 Reference Manual

    Table of Contents Preface, Notes, Licenses . . . . . . . . ....1. Licenses for Third-Party Components ....1.1. FindGTest.cmake License ....1.2. LPeg Library License ....1.3. LuaFileSystem Library License ....

Global site tag (gtag.js) - Google Analytics