Database Administrators
Q&A for database professionals who wish to improve their database skills
Latest Questions
2
votes
2
answers
9600
views
how to find the T-SQL of a sleeping session that is blocking some other process?
I have a [stored procedure called sp_radhe][1] that I put on my servers and it has been helping me to "see" what is happening internally. here is the code of this stored procedure: USE [master] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --DROP PROCEDURE dbo.sp_radhe GO CREATE PROCEDURE dbo....
I have a stored procedure called sp_radhe that I put on my servers and it has been helping me to "see" what is happening internally.
here is the code of this stored procedure:
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--DROP PROCEDURE dbo.sp_radhe
GO
CREATE PROCEDURE dbo.sp_radhe
AS
/*
=======================================================================
Script : SP_RADHE
Author : Marcelo Miorelli
Date : 04 MAR 2013 Wednesday
Desc : shows the current processes
Usage : sp_radhe
-- same as sp_who2
=======================================================================
History
Date Action User Desc
-----------------------------------------------------------------------
27-oct-2014 changed Marcelo Miorelli commented out the line --and es.status = 'running'
so the procedure returns any es.status
=======================================================================
*/
--======================================
-- describe primary blocks of processing
--======================================
------------------------------------------------
-- describe action of logical groups of commands
------------------------------------------------
-- describe individual actions within a command set
BEGIN
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT es.session_id AS session_id
,COALESCE(es.original_login_name, 'No Info') AS login_name
,COALESCE(es.host_name,'No Info') AS hostname
,COALESCE(es.last_request_end_time,es.last_request_start_time) AS last_batch
,es.status
,COALESCE(er.blocking_session_id,0) AS blocked_by
,COALESCE(er.wait_type,'MISCELLANEOUS') AS waittype
,COALESCE(er.wait_time,0) AS waittime
,COALESCE(er.last_wait_type,'MISCELLANEOUS') AS lastwaittype
,COALESCE(er.wait_resource,'') AS waitresource
,coalesce(db_name(er.database_id),'No Info') as dbid
,COALESCE(er.command,'AWAITING COMMAND') AS cmd
,sql_text=st.text
,transaction_isolation =
CASE es.transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'Read Uncommitted'
WHEN 2 THEN 'Read Committed'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot'
END
,COALESCE(es.cpu_time,0)
+ COALESCE(er.cpu_time,0) AS cpu
,COALESCE(es.reads,0)
+ COALESCE(es.writes,0)
+ COALESCE(er.reads,0)
+ COALESCE(er.writes,0) AS physical_io
,COALESCE(er.open_transaction_count,-1) AS open_tran
,COALESCE(es.program_name,'') AS program_name
,es.login_time
FROM sys.dm_exec_sessions es
LEFT OUTER JOIN sys.dm_exec_connections ec ON es.session_id = ec.session_id
LEFT OUTER JOIN sys.dm_exec_requests er ON es.session_id = er.session_id
LEFT OUTER JOIN sys.server_principals sp ON es.security_id = sp.sid
LEFT OUTER JOIN sys.dm_os_tasks ota ON es.session_id = ota.session_id
LEFT OUTER JOIN sys.dm_os_threads oth ON ota.worker_address = oth.worker_address
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS st
where es.is_user_process = 1
and es.session_id @@spid
--and es.status = 'running'
GO
exec sys.sp_MS_marksystemobject 'sp_radhe'
GO
However, when a process is being blocked by some other process that is not active, I am struggling to figure it out the T-SQL of the blocking code.
For example:
the session 92 you can see on the above picture is a select
and the session 75 is an update that I left the transaction open.
**session 92**
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT TOP 1000 [accountID]
,[accountCreateKeyID]
,[totalAccountCreditValueLocal]
,[accountCreateDate]
,[createdDate]
,[createdBy]
,[modifiedDate]
,[modifiedBy]
FROM [TableBackups].[dbo].[_AO20150806_crm_build_account_DoNotDelete]
WITH (HOLDLOCK)
**session 75**
BEGIN TRANSACTION T1
SELECT @@TRANCOUNT
update
[TableBackups].[dbo].[_AO20150806_crm_build_account_DoNotDelete]
set [totalAccountCreditValueLocal] = 1000
where accountID = 1
How can I find the code of the blocking session, in this case, the session 75 when the session's status is 'sleeping'?
**New version**
this new version shows also the blocking session, however, I could not find out how to get the database name and other data from a sleeping session.
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT es.session_id AS session_id
,COALESCE(es.original_login_name, 'No Info') AS login_name
,COALESCE(es.host_name,'No Info') AS hostname
,COALESCE(es.last_request_end_time,es.last_request_start_time) AS last_batch
,es.status
,COALESCE(er.blocking_session_id,0) AS blocked_by
,COALESCE(er.wait_type,'MISCELLANEOUS') AS waittype
,COALESCE(er.wait_time,0) AS waittime
,COALESCE(er.last_wait_type,'MISCELLANEOUS') AS lastwaittype
,COALESCE(er.wait_resource,'') AS waitresource
,coalesce(db_name(er.database_id),'No Info') as dbid
,COALESCE(er.command,'AWAITING COMMAND') AS cmd
,sql_text=st.text
,transaction_isolation =
CASE es.transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'Read Uncommitted'
WHEN 2 THEN 'Read Committed'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot'
END
,COALESCE(es.cpu_time,0)
+ COALESCE(er.cpu_time,0) AS cpu
,COALESCE(es.reads,0)
+ COALESCE(es.writes,0)
+ COALESCE(er.reads,0)
+ COALESCE(er.writes,0) AS physical_io
,COALESCE(er.open_transaction_count,-1) AS open_tran
,COALESCE(es.program_name,'') AS program_name
,es.login_time
FROM sys.dm_exec_sessions es
LEFT OUTER JOIN sys.dm_exec_connections ec ON es.session_id = ec.session_id
LEFT OUTER JOIN sys.dm_exec_requests er ON es.session_id = er.session_id
LEFT OUTER JOIN sys.server_principals sp ON es.security_id = sp.sid
LEFT OUTER JOIN sys.dm_os_tasks ota ON es.session_id = ota.session_id
LEFT OUTER JOIN sys.dm_os_threads oth ON ota.worker_address = oth.worker_address
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS st
where es.is_user_process = 1
and es.session_id @@spid
UNION
SELECT es.session_id AS session_id
,COALESCE(es.original_login_name, 'No Info') AS login_name
,COALESCE(es.host_name,'No Info') AS hostname
,COALESCE(es.last_request_end_time,es.last_request_start_time) AS last_batch
,es.status
,COALESCE(er.blocking_session_id,0) AS blocked_by
,COALESCE(er.wait_type,'MISCELLANEOUS') AS waittype
,COALESCE(er.wait_time,0) AS waittime
,COALESCE(er.last_wait_type,'MISCELLANEOUS') AS lastwaittype
,COALESCE(er.wait_resource,'') AS waitresource
,coalesce(db_name(er.database_id),'No Info') as dbid
,COALESCE(er.command,'AWAITING COMMAND') AS cmd
,sql_text=st.text
,transaction_isolation =
CASE es.transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'Read Uncommitted'
WHEN 2 THEN 'Read Committed'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot'
END
,COALESCE(es.cpu_time,0)
+ COALESCE(er.cpu_time,0) AS cpu
,COALESCE(es.reads,0)
+ COALESCE(es.writes,0)
+ COALESCE(er.reads,0)
+ COALESCE(er.writes,0) AS physical_io
,COALESCE(er.open_transaction_count,-1) AS open_tran
,COALESCE(es.program_name,'') AS program_name
,es.login_time
FROM sys.dm_exec_sessions es
INNER JOIN sys.dm_exec_requests ec2 ON es.session_id = ec2.blocking_session_id
LEFT OUTER JOIN sys.dm_exec_connections ec ON es.session_id = ec.session_id
LEFT OUTER JOIN sys.dm_exec_requests er ON es.session_id = er.session_id
LEFT OUTER JOIN sys.server_principals sp ON es.security_id = sp.sid
LEFT OUTER JOIN sys.dm_os_tasks ota ON es.session_id = ota.session_id
LEFT OUTER JOIN sys.dm_os_threads oth ON ota.worker_address = oth.worker_address
CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) AS st
where es.is_user_process = 1
and es.session_id @@spid

Marcello Miorelli
(17274 rep)
Aug 20, 2015, 01:59 PM
• Last activity: Jul 4, 2024, 07:28 PM
4
votes
1
answers
710
views
What are the ramifications of long open sessions?
I'm looking after a SQL server having long open sessions. There is no open transaction, or actively running query (requests) for this session. 1. What is the reason why a session may remain open? Is it because a transaction is opened and not closed? 2. What are the ramifications of having such an op...
I'm looking after a SQL server having long open sessions.
There is no open transaction, or actively running query (requests) for this session.
1. What is the reason why a session may remain open? Is it because a transaction is opened and not closed?
2. What are the ramifications of having such an open session?
variable
(3590 rep)
Jan 9, 2024, 12:12 PM
• Last activity: Jan 9, 2024, 01:14 PM
1
votes
2
answers
1021
views
stored procedure runs fine in all sessions but one
There are procedures that run fine manually [but not in a job][1], or fails when run [from an application][2], or [not work in SSIS SQL task][3] Mine works in all sessions but one. this is the code I am running - it calls a stored procedure that gets the trigger definition(s) and save it in a temp t...
There are procedures that run fine manually but not in a job , or fails when run from an application , or not work in SSIS SQL task
Mine works in all sessions but one.
this is the code I am running - it calls a stored procedure that gets the trigger definition(s) and save it in a temp table.
works fine and it is part of my automation work.
IF OBJECT_ID('tempdb.dbo.#Jagannatha_sp_getTriggerDef') IS NOT NULL
DROP TABLE #Jagannatha_sp_getTriggerDef
CREATE TABLE #Jagannatha_sp_getTriggerDef (
DB sysname not null,
parent_name nvarchar(600) not null,
object_id int not null,
trigger_name sysname not null,
is_disabled bit,
i int not null,
[trigger_definition] NVARCHAR(MAX) not null,
primary key clustered (DB,trigger_name,i))
truncate table #Jagannatha_sp_getTriggerDef
exec sp_getTriggerDef @dbname = 'APCore',
@TableName = 'dbo.receivedLog',
@Drop_ONly = 0,
@Radhe = '#Jagannatha_sp_getTriggerDef'
SELECT *
FROM #Jagannatha_sp_getTriggerDef
order by db,i
I run it on a table that has no triggers - just to make it as simple as possible
it comes out with the warning
IF 'my_server\_DEVELOPMENT' @@ServerName THROW 50001, 'Wrong Server!!!',1
all good.
but then on this particular session:
and there is nothing different in this session, that I could spot so far.
SELECT *
FROM sys.dm_exec_sessions
where login_name = 'my_company\my_user'
and session_id = @@SPID
the second one is where it fails.
all others it works fine.
what can I do to find out what is different?
or even better, change the procedure so that it would work despite the difference?



Marcello Miorelli
(17274 rep)
May 26, 2022, 07:13 AM
• Last activity: Jul 7, 2022, 08:04 AM
5
votes
1
answers
342
views
How does one map the client_version value (from sys.dm_exec_sessions) to TDS Version?
From Microsoft's SQL Server documentation of [sys.dm_exec_sessions][1] client_interface_name Name of library/driver being used by the client to communicate with the server. datatype: nvarchar(32) client_version TDS protocol version of the interface that is used by the client to connect to the server...
From Microsoft's SQL Server documentation of sys.dm_exec_sessions
client_interface_name
Name of library/driver being used by the client to communicate with the server.
datatype: nvarchar(32)
client_version
TDS protocol version of the interface that is used by the client to connect to the server.
datatype: int
From Microsoft's TDS (Tabular Data Stream) Protocol message and product behavior documentation
TDSVersion
The highest TDS version being used by the client.
The client sends a standard LOGIN7 message to the server to discover the server's highest TDS version.
datatype: binary
Ben Gribaudo posted a partial value map and suggests that client_version is, in fact, not the actual TDS Version.
Questions
(1) What is the client_version value for?
(2) If the client_version definition (above) is correct, then where is the official documented map of integer values to TDS description (either string or binary values)?
EngineeringSQL
(579 rep)
Mar 5, 2022, 02:09 PM
• Last activity: Mar 7, 2022, 07:39 PM
Showing page 1 of 4 total questions