导入trace文件到数据库表

该SQL存储过程用于将trace文件的内容导入到数据库表中。它首先检查表是否存在,如果存在则创建新的表,然后利用`fn_trace_gettable`函数读取指定路径的trace文件,并将数据插入新表。最后输出导入的行数和相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值