Capture all statements for a SQL Server session

本文介绍了一种在SQL Server中创建服务器端跟踪的方法,通过跟踪特定会话的所有已完成批次,帮助用户保存重要的查询并避免丢失。该方法涉及创建两个存储过程来启动和停止跟踪。

 

Capture all statements for a SQL Server session

Written By: Edgewood Solutions Engineers -- 3/18/2009 -- 0 comments

     Stay informed - get the MSSQLTips.com newsletter and win - click here    

Problem
One thing that frustrates me is that sometimes I write these great queries, but often forget to save them or can't remember exactly what the query looked like that I ran five iterations ago.  One smart thing to do would be to always save your scripts, but when your in the process of trying a bunch of different things there is always the chance you may loose something.  In this tip we look at creating a server side trace for one session and capturing all completed batches that are run, so you can find that great query that you thought you may have lost.

Solution
The first thing that needs to be done is to create two stored procedures, which I created in my master database.  These allow you to create a server side trace and also turn off a server side trace.   For more information about a server side trace refer to this past tip, SQL Server Performance Statistics Using a Server Side Trace.


Starting the Trace

The first stored procedure shown below, starts the server side trace, but it also excepts the SPID for the particular session you want to capture.  It will create a file name such as "TraceMySession_52_d20090317120912.trc" to keep the trace session unique.  This is set to save in the "C:" drive of the server, so you may want to change this to a different directory.  The script is also setup to create a 5MB file and then roll over to additional files.  Again you can check out the tip mentioned above for more about these settings.

The other thing that we are doing is only capturing the SQL:BatchCompleted events.  This way the trace file doesn't get very large.  The three data columns that will be captured are TextData, StartTime and SPID.

To create the procedure, copy the code below and execute it.  Like I said I created this in the master database, but this could be created in a different database.

CREATE PROCEDURE [dbo].[spTraceMySessionStart] @spid INT
AS

-- Create a Queue
DECLARE @rc INT
DECLARE 
@TraceID INT
DECLARE 
@maxfilesize bigint
SET @maxfilesize 
DECLARE @filename NVARCHAR(245)
SET @filename 'C:/TraceMySession_' 
    
CONVERT(NVARCHAR(10),@spid
    + 
'_d' 
    
REPLACE(CONVERT(VARCHARGETDATE(),111),'/',''
    + 
REPLACE(CONVERT(VARCHARGETDATE(),108),':','')

EXEC @rc sp_trace_create @TraceID output2@filename@maxfilesize, NULL 
IF (@rc != 0GOTO error

-- Set the events
DECLARE @on bit
SET @on 1
EXEC sp_trace_setevent @TraceID121@on
EXEC sp_trace_setevent @TraceID1212@on
EXEC sp_trace_setevent @TraceID1214@on

-- Set the Filters
DECLARE @intfilter INT
DECLARE 
@bigintfilter bigint
EXEC sp_trace_setfilter @TraceID1210@spid

-- Set the trace status to start
EXEC sp_trace_setstatus @TraceID1

-- display trace id for future references
SELECT TraceID=@TraceID
GOTO finish

error: 
SELECT ErrorCode=@rc

finish: 

Stopping the Trace

This stored procedure below will stop and close the trace once you are finished.  If this is not run the server side trace will continue to run and collect data for any other session that happens to have the same SPID.

This stored procedure excepts one parameter which is TraceID.  When you actually run the first stored procedure it will give you the TraceID that was created.  This is the value that you pass to this second stored procedure to stop and close the trace.

I also created this in my master database, but this can be created in a different database as well.

CREATE PROCEDURE [dbo].[spTraceMySessionStop] @traceId INT
AS
EXEC 
sp_trace_setstatus @traceId,0
EXEC sp_trace_setstatus @traceId,2

Let's Try It

So let's say we have the following code that we want to run.

EXEC master.dbo.spTraceMySessionStart 52

USE AdventureWorks
GO

SELECT name
FROM sys.sysobjects WHERE xtype 'U'
GO
SELECT TOP 10 Title
FROM HumanResources.Employee
GO

EXEC master.dbo.spTraceMySessionStop 2

 The first thing that we need to do is start the trace which is the first line of my script.  I can see my SPID at the bottom of the screen which is 52, which has been highlighted below.

When I run the store procedure, the output shows me the TraceID which is "2".  We need to keep this value for later.

After I have run my other commands and I am done, I can run the last line to stop and close the trace and use the TraceID value of "2" which we got above.


Reviewing What Was Captured

If I open the file that was created using Profiler I can see all of the commands that were run for my session.  You could then copy these lines into a query window for reuse.

Another option is to load the file that was created to a SQL Server table or just query the data using SQL Server. The steps for doing this can be found in this tip.

SELECT TEXTDataStartTime
FROM ::fn_trace_gettable('c:/TraceMySession_52_d20090317120542.trc'DEFAULT)
WHERE TEXTData IS NOT NULL 

 

Next Steps

  • Hopefully this gives you another tool you can use to capture all of the batches that are run for a session for future reference.
  • Try changing this to add other columns or events
  • Also change this to capture multiple SPIDs at once
  • Refer to this tip for more information on creating a Server Side Trace
基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动方法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该方法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该方法的有效性和先进性。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对方法泛化能力的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值