简体   繁体   English

SQL Server会话查询

[英]SQL Server session queries

To be able to access a session query all the results that I found during my research is about the last query executed in a SQL Server session. 为了能够访问会话查询,我在研究过程中发现的所有结果都与SQL Server会话中执行的最后一个查询有关。

Is there a way to access all the queries that has run inside session instead of SQL Server profiler because to use profiler in prduction environment is not that easy. 有没有一种方法可以访问在会话中运行的所有查询而不是SQL Server事件探查器,因为在生产环境中使用事件探查器并不是那么容易。

Any helps? 有帮助吗?

I possibly found here a solution to your problem. 我可能在这里找到了解决您问题的方法。 I added a cursor to get all queries for a specific session: 我添加了一个游标以获取特定会话的所有查询:

DECLARE @sql TABLE(sql_handle VARBINARY(128))
DECLARE @sqltext VARBINARY(128)

INSERT INTO @sql
SELECT sql_handle
FROM sys.sysprocesses p
WHERE p.spid = (YOUR_SESSION_ID)

DECLARE MyCursor CURSOR FOR
SELECT sql_handle 
FROM @sql
OPEN MyCursor
FETCH NEXT FROM MyCursor
INTO @sqltext
WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT TEXT
        FROM sys.dm_exec_sql_text(@sqltext)
        FETCH NEXT FROM MyCursor INTO @sqltext
    END
CLOSE MyCursor
DEALLOCATE MyCursor;
GO

I hope this is what you are searching for. 我希望这是您要寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM