if exists(select * from sysobjects where xtype='P' and name='up_trace_createTableFromFile')
drop procedure up_trace_createTableFromFile
go
CREATE procedure dbo.up_trace_createTableFromFile
@pathfile nvarchar(256),
@numfiles int=-1, --指定要读取的翻转文件数,包括在 filename 中指定的初始文件
@message nvarchar(4000) output --
AS
/**********
--文件名 :
--用途 :导入trace文件到数据库表
--输入参数 :
--返回值解释 :
--创建者 : summer.yang
--创建日期 : 2005-6-18
--修改者 :
--修改日期 :
--修改注解 :(引用请保留此信息)
--备注说明 : number_files 默认值-1表示读取全部翻转文件直至跟踪结束。
**********/
--需要判断输入的文件和路径是否存在
--导入文件到变量表,只获取需要用到的数据
--需要修改分析死锁的过程的判断开始和结束的语句,只获取最早和最晚的时间
set nocount on
set @pathfile=ltrim(rtrim(@pathfile))
-------
declare @return int
set @return=0
set @message=''
-------
declare @tracetable sysname
declare @rows int
set @tracetable=right(@pathfile,charindex('',reverse(@pathfile),1)-1)
set @tracetable=left(@tracetable,charindex('.',@tracetable,1)-1)
set @tracetable=ltrim(rtrim(@tracetable))
if exists(select * from sysobjects where xtype='U' and name=@tracetable)
begin
set @tracetable=@tracetable+'_'+datename(hh,getdate())+datename(mi,getdate())
end
declare @sqlstring nvarchar(4000)
set @sqlstring='
create table dbo.'+@tracetable+' (
RowNumber int not null identity primary key,
[EventClass] [int] NOT NULL ,
[TextData] [ntext] ,
[SPID] [int] NULL ,
[Duration] [bigint] NULL ,
[StartTime] [datetime] NULL ,
[EndTime] [datetime] NULL ,
[Reads] [bigint] NULL ,
[Writes] [bigint] NULL ,
[CPU] [int] NULL ,
[ClientProcessID] [int] NULL ,
[ApplicationName] [nvarchar] (128) NULL ,
[LoginName] [nvarchar] (128) NULL ,
--[ServerName] [nvarchar] (128) NULL ,
[HostName] [nvarchar] (128) NULL ,
[NTUserName] [nvarchar] (128) NULL ,
[DatabaseID] [int] NULL ,
[ObjectID] [int] NULL ,
[IndexID] [int] NULL ,
[IntegerData] [int] NULL ,
[Mode] [int] NULL )
insert into dbo.'+@tracetable+'
select [EventClass], [TextData], [SPID], [Duration], [StartTime], [EndTime], [Reads], [Writes], [CPU], [ClientProcessID],
[ApplicationName],[LoginName],[HostName], [NTUserName],[DatabaseID], [ObjectID],[IndexID],[IntegerData], [Mode]
from ::fn_trace_gettable(@pathfile,@numfiles)
select @rows=count(*) from dbo.'+@tracetable+'
'
exec @return=sp_executesql @sqlstring ,
N'@pathfile nvarchar(256),@numfiles int,@rows int output',
@pathfile,@numfiles,@rows output
set @message= '祝贺! '+char(10)+'成功生成trace表:'+@tracetable+char(10)
+'Trace文件是: '+@pathfile+char(10)
+'此表共计行数为:'+ltrim(str(@rows))
print @message
-------
GO