首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sqlserver使用存储过程跟踪SQL

sqlserver使用存储过程跟踪SQL

作者头像
易墨
发布2018-09-14 15:48:34
1.6K0
发布2018-09-14 15:48:34
举报

为跟踪EF生成的SQL

/1.添加存储过程/

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_start] Script Date: 2017/7/6 19:48:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO

create PROC [dbo].[sp_perfworkload_trace_start] @dbid AS INT, @tracefile AS NVARCHAR(254), @traceid AS INT OUTPUT AS -- Create a Queue DECLARE @rc AS INT; DECLARE @maxfilesize AS BIGINT;

SET @maxfilesize = 5;

EXEC @rc = sp_trace_create @traceid OUTPUT, 0, @tracefile, @maxfilesize, NULL IF (@rc != 0) GOTO error;

-- Client side File and Table cannot be scripted

-- Set the events DECLARE @on AS BIT; SET @on = 1; EXEC sp_trace_setevent @traceid, 10, 15, @on; EXEC sp_trace_setevent @traceid, 10, 8, @on; EXEC sp_trace_setevent @traceid, 10, 16, @on; EXEC sp_trace_setevent @traceid, 10, 48, @on; EXEC sp_trace_setevent @traceid, 10, 1, @on; EXEC sp_trace_setevent @traceid, 10, 17, @on; EXEC sp_trace_setevent @traceid, 10, 10, @on; EXEC sp_trace_setevent @traceid, 10, 18, @on; EXEC sp_trace_setevent @traceid, 10, 11, @on; EXEC sp_trace_setevent @traceid, 10, 12, @on; EXEC sp_trace_setevent @traceid, 10, 13, @on; EXEC sp_trace_setevent @traceid, 10, 14, @on; EXEC sp_trace_setevent @traceid, 45, 8, @on; EXEC sp_trace_setevent @traceid, 45, 16, @on; EXEC sp_trace_setevent @traceid, 45, 48, @on; EXEC sp_trace_setevent @traceid, 45, 1, @on; EXEC sp_trace_setevent @traceid, 45, 17, @on; EXEC sp_trace_setevent @traceid, 45, 10, @on; EXEC sp_trace_setevent @traceid, 45, 18, @on; EXEC sp_trace_setevent @traceid, 45, 11, @on; EXEC sp_trace_setevent @traceid, 45, 12, @on; EXEC sp_trace_setevent @traceid, 45, 13, @on; EXEC sp_trace_setevent @traceid, 45, 14, @on; EXEC sp_trace_setevent @traceid, 45, 15, @on; EXEC sp_trace_setevent @traceid, 41, 15, @on; EXEC sp_trace_setevent @traceid, 41, 8, @on; EXEC sp_trace_setevent @traceid, 41, 16, @on; EXEC sp_trace_setevent @traceid, 41, 48, @on; EXEC sp_trace_setevent @traceid, 41, 1, @on; EXEC sp_trace_setevent @traceid, 41, 17, @on; EXEC sp_trace_setevent @traceid, 41, 10, @on; EXEC sp_trace_setevent @traceid, 41, 18, @on; EXEC sp_trace_setevent @traceid, 41, 11, @on; EXEC sp_trace_setevent @traceid, 41, 12, @on; EXEC sp_trace_setevent @traceid, 41, 13, @on; EXEC sp_trace_setevent @traceid, 41, 14, @on;

-- Set the Filters DECLARE @intfilter AS INT; DECLARE @bigintfilter AS BIGINT; -- Application name filter EXEC sp_trace_setfilter @traceid, 10, 0, 7, N'SQL Server Profiler%'; -- Database ID filter EXEC sp_trace_setfilter @traceid, 3, 0, 0, @dbid;

-- Set the trace status to start EXEC sp_trace_setstatus @traceid, 1;

-- Print trace id and file name for future references PRINT 'Trce ID: ' + CAST(@traceid AS VARCHAR(10))

  • ', Trace File: ''' + @tracefile + '''';

GOTO finish;

error: PRINT 'Error Code: ' + CAST(@rc AS VARCHAR(10));

finish:

/2.创建文件跟踪/

declare @dbid int; declare @traceid int; SET @dbid = DB_ID(); EXEC dbo.sp_perfworkload_trace_start @dbid = @dbid, @tracefile = 'D:\sqltrc\20170706_6', @traceid = @traceid OUTPUT;

/3.写入trc文件/

EXEC sp_trace_setstatus 2, 0; EXEC sp_trace_setstatus 2, 2;

/4. 查询日志/

SELECT * FROM sys.fn_trace_gettable( 'D:\sqltrc\20170706_6.trc', NULL) AS t ORDER BY StartTime desc

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 为跟踪EF生成的SQL
    • /1.添加存储过程/
      • /2.创建文件跟踪/
        • /3.写入trc文件/
          • /4. 查询日志/
          相关产品与服务
          云数据库 SQL Server
          腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档