简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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