Transact SQL :: How To Keep Track Of Blocked / Deadlocked Queries
Jun 2, 2015
Is there a way to write a script ,keep the script in SQL Agent job , run the  job to keep track of Blocked and deadlocked queries and store them in any Table or with triggers?with out Profiler,Audit,activitymonitor ?Â
I have these two CTE queries, the first one is the existing one which runs fine and returns what we need, the second is a new CTE query which result I need to join in to the existing CTE, now that would be two CTE's can that be done at all?The second query is working on the same table as the first one so there are overlaps in names, and they are the same, only columns I need to "join" is the "seconds" and "AlarmSessions".
;with AlarmTree as ( select NodeID, ParentID, NodeLevel, NodeName, cast('' as varchar(max)) as N0, cast('' as varchar(max)) as N1, cast('' as varchar(max)) as N2, cast('' as varchar(max)) as N3,
I have 2 tables, i need to take the max date from PAY and Combine in MENÂ
MEN ====== id  |  Fname  ======== 1  |  AAA 2    |  BBB 3    |  CCC
PAY === id   |   Tdate ========= 1   |  01.01.2015 1   |  02.01.2015 1   |  03.01.2015 2   |  06.01.2015 3   |  09.01.2015 3   |  10.01.2015i
I need to show this:
id  |  Fname  |  Tdate ============= 1  |  AAA    |   03.01.2015 2 |  BBB    |   06.01.2015 3  |  CCC    |  10.01.2015
As a part of DBA, I used to execute various SQL files. Most of the time, it is like a manual effort to execute the files individually.
I am looking to automate the process, like a single click to execute all the .SQL files.
The main hurdle I have is, some files needs to be executed in A1 database, some in B1 database and some other SQL files need to be executed in C1 database. In this scenario, I need to pass the DBName information to the powershell query dynamically.
My design for this requirement is, say each .SQL file need to contain a template like
@DBName = 'your Database name' @Executeon = 'When to execute'
In this case, the powershell first need to read the SQL file and finds the value for @DBName and replace it in the powershell query and execute the SQL files automatically.
Is it feasible ? Or any other alternate easier way to proceed.
I have 2 DBs located on separate Sql Servers but the DBs are linked. I am querying data from both DBs but want to combine the results. Here is my query but it doesn't seem to be working.
(SELECT DISTINCT idname, name, address, address2, awardedtoname, suppno FROM contract INNER JOIN house ON contract.idname = house.idname) JOIN (SELECT DISTINCT tpd.PropertyNumber AS [Property No], tpd.Address1 + ' , ' + tpd.Address2 AS Estate, tpd.Address1 AS Address1,
I have these two queries I would like to join, however the later is a grouped query how can I join it with the first query? Has to be joined on EventId. The second query is a total table scan.
SELECT AH.EventID, AH.TechnicalAddress, AH.AlarmAlias, AH.AlarmPath as [OrgAlarmPath], AH.AlarmCounter as AlarmCount, AH.EventDateTime as EventTime, AH.[Priority], AH.AlarmMessage, AH.EventText, AH.CallListName, AH.AlarmReadDate as EndTime, AH.alh_EventEndedUserRemark as [EndRemark] --, SUM(seconds) here, and AlarmSessions here FROM AlarmHistory AH
[Code] ...
2) WHERE ia.EventTypeId = 0 group by ia.EventId order by EventId desc
I have to run this 3 times for similar but slightly different parameters. I only change the very top peice of code slightly each time:
1. Partition by Forename, Surname, DOB, Postcode 2. Partition by Forename, DOB, Postcode 3. Forename, Surname, DOB.
As you can see very subtle change, ideally I'd like to find a way to run just one report where all of the above occur, the issue I face running separately is one person may appear on 1 or more giving duplicates.
USE HealthBI_Views;this bit below is basically grouping my output on Forename, Surname, DOB & Postcode. What the whole process is trying to achieve is to show where a patient which has all of the above fields identical but has a different patient identifier suggesting that the service has allocated to unique identifiers to the same person which will result in both records needing to be merged.
WITH cte AS (SELECT *, COUNT(HEYNo) OVER ( PARTITION BY Forename, Surname, DOB
[code]...
--- this bit below is simply showing all instances where the above criteria is met and there are more then one instances.WHERE countOfHeyNo > 1 --- The final output display all patient identifiable information from the MF_PATIENT table so that the report can be created in SSRS and run routinely by the Data Quality Team who can then investigate each occurance.
SELECT EventID, Role, EventDuty, Qty, StartTime, EndTime, Hours FROM dbo.tblEventStaffRequired;
and SELECT EventID, Role, StartTime, EndTime, Hours, COUNT(ID) AS Booked FROM tblStaffBookings GROUP BY EventID, Role, StartTime, EndTime, Hours;
How can I join the results of the two by matching the columns EventID, Role, StartTime and EndTime in the two and have the following columns in output EventID, Role, EventDuty, Qty, StartTime, EndTime, Hours and Booked?
I have a table (let's call it MyTable) that consists of four fields:
Id, Source, FirstField, and SecondField, where Source only takes one of two values: Source1 and Source2. The records in this table look as follows:
Id Source FirstField Secondfield
1 Source1 Product 3 name Product 3 description
[code]...
I need to return, using 3 different T-SQL queries:
1) Products that exist only in Source2 (in red above) 2) Products that exist only in Source1 (in green above) 3) Products that exist both in Source1 and Source2 (in black above)
For 1) so far I've been doing something along the lines of SELECT * FROM MyTable WHERE Source=Source1 AND FirstField NOT IN (SELECT DISTINCT (FirstField) FROM MyTable WHERE Source=Source2)
I have read about INTERSECT and EXCEPT, but I am a little unclear if they could be applied in this case out of the box.
I have a table of raw data where each column can be null. The thought was to create an identity key (1,1) and set as primary for each row. (name/ address / zip/country/joindate/spending) with surrogate key: "pkid".However other queries will not use this primary key. So for instance they may count the # of folks at a zip, select all names, addresses etc. The queries may order by join date, or select all the people that joined on a specific date.No other code would logically use the primary key (surrogate primary id key), therefore would it still have any performance benefits? at this time the table would have no clustured or nonclustured indexes or keys. I'm curious if there are millions of records.
When I execute a select with an inner join my query is bombing out calling me a deadlock victim. I know what deadlocking is, but I am only doing a select. Why would a select lock records?
Could anyone explain what locking occurs when a select is fired with a join?
Hi, I got the following error when I try running my “comments.aspx� page with visual studio 2005Exception Details: System.Data.SqlClient.SqlException: Transaction (Process ID 83) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. fExecuteQuery(String commandText, String dataSetName) +90 fExecuteQuerySet(String commandText, String dataSetName) +36 ASP.comments_aspx.GetNarComment() +618 ASP.comments_aspx.Page_Load(Object sender, EventArgs e) +476 System.Web.UI.Control.OnLoad(EventArgs e) +67 System.Web.UI.BasePage.OnLoad(EventArgs e) +1013 System.Web.UI.PopupPage.OnLoad(EventArgs e) +4 System.Web.UI.Control.LoadRecursive() +35 System.Web.UI.Page.ProcessRequestMain() +750 The segment code was the problem sits in file "comments.aspx":: … string cmdText=��; cmdText = string.Format(@"-- Get All Narative comments fo all students in the course from @selectedTermID down to its child terms exec aagGetStudentSectionComments @companyID={0}, @sectionID={1}, @selectedTermID={2}, @StudentID={3} ", _companyID, sectionID, selectedTermID, studentID);
ds = fExecuteQuerySet(cmdText, "getMySet");
… // the 2 functions to deal with ADO.NET to be called in above code segment
// return a dataset. public DataSet fExecuteQuery(string commandText, string dataSetName) { DataSet mds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(commandText, _cn); da.SelectCommand.CommandTimeout = 600; // 600 seconds da.Fill(mds, dataSetName); return mds; // return dataset } // Assume para commandText contains sql query which returns a table or more. // return a DataSet. public DataSet fExecuteQuerySet(string commandText, string dataSetName) { DataSet mds = new DataSet(); mds = fExecuteQuery(commandText, dataSetName); return mds; // return DataSet }
Please give me the reason why that dealock happens?Thanks in advance
The errormessage after a deadlock gives very little information: "Your transaction (process ID #12) was deadlocked with another process and has been chosen as the deadlock victim. Rerun your transaction."
But what resources, ie tables, indexes etc, are really deadlocked?
Is it possible to set a switch or something to log all relevant information about a deadlock for later investigation?
ive seen this Deadlock Error message out on the internet being discussed, but no solution being offered. i have a windows service that's running Select Statements [one at a time] - so unless there's some command in sql server that would re-run these - it could be a problem for me. now if im running this select proc manually - of course i see the message and re-run the process, but how can this be accomplished programatically. see msg below:
Transaction (Process ID 106) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Hi, i get this error while i manually execute dts. But when i execute dts on my .aspx page, i can't handle this error on "... catch(Exception ex) {... }" part. catch (Exception ex) { return ex.Message ; //DtsPackage.OnError += new PackageEvents_OnErrorEventHandler(DtsPackage_OnError); } Here is dts message in a text file. Step 'DTSStep_DTSDataPumpTask_3' failedStep Error Source: Microsoft OLE DB Provider for SQL ServerStep Error Description:Transaction (Process ID 124) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.Step Error code: 80004005Step Error Help File:Step Error Help Context ID:0 Any idea?
Sql version Microsoft SQL Server 2000 - 8.00.818 (Intel X86) May 31 2003 16:08:15 Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
I have dts running 24 X 7 every 5 minutes I noticed DTS failed few times a day with error
Step Error Source: Microsoft OLE DB Provider for SQL Server Step Error Description:Transaction (Process ID 74) was deadlocked on thread | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Step Error code: 80004005 Step Error Help File: Step Error Help Context ID:0
The is no entry about in Server server log files. will trace identify deadlock and why deadlock info not entred into log file ?
;) Hello Everybody, My name is Fabio and I post from Italy. First, I don't know if this argument was already discussed in the past, but I'm new in this group so ... Second I'm not so expert in DB due to the fact that I'm using SQL for the first time in my life ...
I use a store procedure to pass to every single user in my intranet (more than 150), details of different clients taken from an SQL table containing around 30.000 names. Users have an ASP page displaying the information Selected in the DB. This means that 150 users display info of 150 different clients.
To to this I use this code in store procedure:
CREATE PROCEDURE sp_assign_name @iduser int AS if exists(select top 1 * from recallornotes where tmkoperator= @iduser) update nominativitelecom set tmkmotrecall=convert(nvarchar(1), tmkstatus), tmkstatus=7 where id in (select top 1 id from recallornotes where tmkoperator=@iduser) else begin if exists(select top 1 id from nonotes) update nominativitelecom set tmkmotrecall=convert(nvarchar(1), tmkstatus), tmkstatus=7, tmkoperator =@iduser where id in (select top 1 id from nonotes with (UPDLOCK) order by NewID()) end GO
This is working quite well when the number of users are more ore less around 50/60, when the number grows, on the IIS server (Pentium IV server, with Win 2000 in English, MS SQL 2000, and 1 Giga of ram), a file called DLLHOST.exe start to use the 100% of the CPU, and the users cannot display any other ASP page on their screens. It is not a virus (some newsgroup report this problem connect to a worm virus, but we have latest antivirus files installed and spyware detect/delete on). The SQL log reports this error:
"transaction (process id 69) was deadlocked on (lock) resources with another process and has been chosen as the deadlock victim. rerun the transaction".
Is there a way to avoid the conflict that occur when different users are trying to select the same record in the DB ? In other terms, which process will you use in the same situation to select one record per user ?
please throw in any ideas as it matter of urgency.
We have rebuild our 24/7 heavy loaded website using ASP.NET 2.0 and SQL 2005 (we have one active-passive cluster for main activity and 2 active-active clusters to browse static data).
Main cluster is publisher and distributor for replication of data (small subset of it and it works fine).
Hi, i get this error while i manually execute dts. But when i execute dts on my .aspx page, i can't handle this error on "... catch(Exception ex) {... }" part. catch (Exception ex) { return ex.Message ; //DtsPackage.OnError += new PackageEvents_OnErrorEventHandler(DtsPackage_OnError); } Here is dts message in a text file.
Step 'DTSStep_DTSDataPumpTask_3' failed
Step Error Source: Microsoft OLE DB Provider for SQL Server Step Error Description:Transaction (Process ID 124) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Step Error code: 80004005 Step Error Help File: Step Error Help Context ID:0
1. I have dropped 10 tables with each around 1-2 gb in DB ABC 2. I had run DBCC ShrinkDatabase (ABC, 20) and it is failed after running 133 hours this morning. Yes, 133 hours.
It ran 72 hours last month and shrinked from 200 gb to 180 gb. Thus, I expected it should be <= 72 hours to fnish since 10 more tables are dropped ?
Msg 1205 Transaction (Process ID 75) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
DBCC shrinkDatabase will cause deadlocking? how to avoid it? Is there other ways to speed up?
Hi, i am getting this error when i am running a stored procedure.
Transaction (Process ID XXXX) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
i think so it is getting this error becasue it blocking it self at one point in the SP
DECLARE cty_Cursor CURSOR FOR SELECT Country FROM TB_Country
declare @cty varchar(2)
OPEN cty_Cursor; FETCH NEXT FROM cty_Cursor into @cty; WHILE @@FETCH_STATUS = 0 BEGIN EXEC SP_DO_SOMETHING @cty FETCH NEXT FROM cty_Cursor into @cty; END; CLOSE cty_Cursor; DEALLOCATE cty_Cursor;
i think so it calls the SP then before SP finsih its working it calls it back from cursor with other argument.
how we can make it sure it finish it execution before it is being called again. i think so we need some sort of lock here but i am not able to find right solution . please anyone suggest something.
In an SSIS package I am continually getting the same error:
"Transaction (Process ID 58) was deadlocked on thread | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
The package is attempting to do a simple Execute SQL Task. Since this seems like a database and not integration services issue, does anybody have any thoughts or insight into this error and where to begin troubleshooting?
I have a quite big SQL query which would be nice to be used using UNION betweern two Select and Where clauses. I noticed that if both Select clauses have Where part between UNION other is ignored. How can I prevent this?
I found a article in StackOverflow saying that if UNION has e.g. two Selects with Where conditions other one will not work. [URL] ....
I have installed SQL Server 2014 and I tried to use tricks mentioned in StackOverflow's article but couldn't succeeded.
Any example how to write two Selects with own Where clauses and those Selects are joined with UNION?
We are using SQL server 6.5 and currently have about 100 users connections at a given point in time. The application is Visual Basic 5.0 based and it allows users to create MS Word documents from the application. These documents names are stored in a table which basically acts as a reference table. Every time a document needs to be attached to a record this table is called with an insert/update query. This results in an exclusive page/table lock and ends up into a blocked process. This finally results into a major halt for all the system users.
Manual killing of these blocked processes frees up the resources and brings things back to normal although disruptive to the users.
Any clue as to why the blocked processes are not able to free themsevles up ? Are we missing something in our SQL configuration that will help us with unblocking these processes ?
I get the error message below. Books online doesn't say any more. Can anyone explain? I am the only user connected to the db at the time, no jobs are executing.
Cannot shrink log file 2 (log) because all logical log files are in use.
(1 row(s) affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
I'm running a heavy SELECT query using WITH (NOLOCK). This still causes other processes trying to INSERT in one of the tables to get blocked. I thought the locking hint would prevent from blocking other processes?
I am getting the following error message when I tried installing the 2005 Developer Edition from the MSDN kit. I previously installed the 2005 Express Edition, and it worked just fine, so I thought that the install failed as it was at a lower version than the Express Edition. I uninstalled the SQLExpress Edition, however, when I try to install SQL Server 2005 again, I get the following message:
Name: Microsoft SQL Server 2005 Tools
Reason: Your upgrade is blocked. For more information about upgrade support, see the "Version and Edition Upgrades" and "Hardware and Software Requirements" topics in SQL Server 2005 Setup Help or SQL Server 2005 Books Online.
Build version check:
Your upgrade is blocked because the existing component is a later version than the version you are trying to upgrade to. To modify an existing SQL Server component, go to Add or Remove Programs in Control Panel, select the component you would like to modify, and then click Change/Remove.
Name: Microsoft SQL Server 2005 Tools
Reason: Your upgrade is blocked. For more information about upgrade support, see the "Version and Edition Upgrades" and "Hardware and Software Requirements" topics in SQL Server 2005 Setup Help or SQL Server 2005 Books Online.
Build version check:
Your upgrade is blocked because the existing component is a later version than the version you are trying to upgrade to. To modify an existing SQL Server component, go to Add or Remove Programs in Control Panel, select the component you would like to modify, and then click Change/Remove.
I have also installed Visual Studio NET, and I wonder if some component in the Studio app is holding back the SQL Server install. I have uninstalled all SQL components several times already, and I keep on getting this message.
I have an SQL Server 2005 mirroring config with "High availability". Twice over the past four weeks, the principal server started logging to the event log:
"All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT" I checked the database activity monitor and indeed there were lots of processes in DBMIRROR_DBM_EVENT.
The database was totally unresponsive to queries (all got timeouts), and no failover was done.
This are the log entries for all day:
05/04/2008 08:40:44,Server,Unknown,All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT. Process Utilization 0%. 05/04/2008 08:39:44,Server,Unknown,All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT. Process Utilization 0%. 05/04/2008 08:38:44,Server,Unknown,All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT. Process Utilization 0%. 05/04/2008 08:37:44,Server,Unknown,All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT. Process Utilization 0%. 05/04/2008 08:36:44,Server,Unknown,All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT. Process Utilization 0%. 05/04/2008 08:35:44,Server,Unknown,All schedulers on Node 0 appear deadlocked due to a large number of worker threads waiting on DBMIRROR_DBM_EVENT. Process Utilization 0%. 05/04/2008 08:33:49,Server,Unknown,The time stamp counter of CPU on scheduler id 2 is not synchronized with other CPUs. 05/04/2008 03:24:02,Backup,Unknown,Log was backed up. Database: TeleCable<c/> creation date(time): 2007/08/24(13:38:00)<c/> first LSN: 205089:18926:1<c/> last LSN: 209688:18463:1<c/> number of dump devices: 1<c/> device information: (FILE=1<c/> TYPE=DISK: {'\face01f$logbackup'}). This is an informational message only. No user action is required. 05/04/2008 00:00:14,spid24s,Unknown,This instance of SQL Server has been using a process ID of 1700 since 4/9/2008 3:18:54 AM (local) 4/9/2008 1:18:54 AM (UTC). This is an informational message only; no user action is required.
After that, the same entry once per minute.
I restarted the database and everything came to normal.
As you can imagine, the problem is really serious.
Should I forget about mirroring? Anyone has experienced something similar?
I have a stored procedure which generates the next sequence number... it uses SERIALIZABLE Option. procs look something like below..
begin SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION
Sequence generating statement...
COMMIT TRANSACTION set @NextSequenceValue = @NextSequenceValue Return @NextSequenceValue end
For some reason when i call the proc with below parameters to get next sequence number.. its hungs up..
declare @NextSequenceValue int set @NextSequenceValue = 0 exec spGetNextSequence 19, 'LotSequence', @NextSequenceValue output, Null select @NextSequenceValue as NextSequenceValue
When i queried sp_who2 it shows that my processid is blocked by some other processid.. and when i do DBCC INPUTBUFFER (blockingprocessid), the query of blocking processid and my nextsequence generation stored proc is not realted at all..
Can you help shed some light on why my nextsequence generating proc is getting hunged...?
If I kill a blocked process, why does the current activity window still show the process? Both processes, blocking and blocked, are scheduled tasks. Also, the blocked process is still listed as a running task in the manage scheduled task window.