SQL Server timeout while executing stored procedure - sql-server

I am experiencing a problem whereby executing a stored procedure to update a record in a table is resulting in a timeout error. I originally call my stored procedure from within an MVC3 application, which is where I first noticed the error.
However, using the SQL Server Profiler, I was able to copy the code that was generated by ADO.NET and run this directly on the database. I let this query run for approximately 5 minutes and it still didn't manage to return anything.
Here are a few facts:
The stored procedure has approximately 100 arguments that are being passed to it.
My MVC Application, SSMS and Sql Server 2008 are all installed on the same machine.
The stored procedure attempts to update a single row in a table containing about 5000 entries.
There was a trigger that would update the LastModifiedDate and CreatedDate, but I removed these triggers, and updated the EDMX to determine if there was an infinite loop caused by these triggers.
Our live server runs exactly the same stored procedure (using classic asp) as the one I am trying to run and achieves the correct result. Furthermore, the live server fails to run the same stored procedure under .NET
My machine fails to run the stored procedure for both the classic ASP and the ASP.NET
The stored procedure seems to fail only for a few of the rows, and others work perfectly fine.
I have tried changing the values of the parameters that are passed into the stored procedure
Other stored procedures work fine
There appears to be a lock on the particular table that the stored procedure was attempting to update, since executing other queries worked fine, even when waiting for this one to execute.
If anyone has any ideas on any other tests I could perform,or any tools I could use to determine the root cause of the timeout error.
Thanks.
P.S. Don't tell me to change the command timeout property, I have tried setting this to zero!

I can think of two things :
I assume you have already implemented exception handling in the stored procedure. If not, please do so, and try to get hold of the problem statement first. When you say, it happens for some rows, it might be due to bad data ? Read this for information on how to do exception handling in SQL Server 2008
Have your tried finding out whether there is a dead-lock ?
Please read this for detailed procedure and understanding.

Related

SQL Server SPIDS go into a sleeping state and never recover

I have a long running stored procedure that is executed from IIS. On average this stored procedure takes between two and five minutes to complete because it is searching through a large dataset. (although it has take around 20 minutes in some cases)
Most of the time the stored procedure works fine but every now and then the SPIDS go into a sleeping state and never recover. The only solution I have found is to restart the SQL Server and re-run the stored procedure
The are no table inserts in the proc (only table variable inserts), and the other statements are selects on a large table.
I'm stuck for where to start debugging this issue. Any hints one what it might be or suggestions on tools that would help me find the issue would be most helpful
EDIT: More info added:
The actual issue is the proc doesn't return the resultset. My first thought was to look at the spids, they were sleeping but the cputime was still increasing
It's a .Net app so .Net Core 3.1 with ASP.NET Core and a Blazor UI. The libary used for db connection is System.data.SqlClient I believe System.data.SqlClient uses it's own custom driver. Calling code below:
The stored procedure doesn't return multiple result sets, however obviously different instances of the proc run at the same time.
No limits to connection pooling in IIS
#RichardWatts when you say " re-run the stored procedure" you mean that the same stored proc with the same parameter and data works once you restart SQL Server ?
If so look over your loc (sp_loc} inside your table probably another process loc some data and doesnt release it properly, specialy if you have transaction accessing the same tables.
What is your your isolation level on your connexion ? If you can, try to change it to READ UNCOMMITTED to see if that solve your problem.
as an alternate you can also add a WITH (NOLOCK) or (READUNCOMMITTED) to your sql command.
Know that you will need to hold query with a read uncommited or nolock if you have some modification on the structure of your table or index re construction for example or they will in turn block its execution
Nevertheless be cautious this solution depend on your environment, specially if your tables gots lots of update, delete, insert,... this kind of isolation can lead to a Dirty read and doesnt adress the root cause of your problem wich I would bet is uncomited transaction (good article that explain it)
Make also a DBCC CHECKTABLE just to be sure on this side

Stored procedure weird error in SQL Server

In SQL Server 2016, a stored procedure is throwing an error when it's executed from my .Net application. And when we execute the same stored procedure in the database with the same input parameters, there is no error.
After altering the stored procedure without changing any logic, it again starts work normally for sometime. Frequently we are facing this issue.
Any suggestion to fix this issue permanently?
We ran into the same exact thing. The stored proc ran fine in the db but took forever or timed out when ran from the app.
In the end it came down to parameter sniffing and having bad plans in the cache. The quick and dirty fix is to recompile the stored proc using the 'with recompile' option. We eventually just re-wrote the proc.
Anyways, this article is very good at explaining how SQL Server uses parameter sniffing, why it is a good thing until it is not.
Happy hunting!

Columns invalid (suddenly) in stored procedure in SQL Server

While I'm relatively new to SQL Server, I've been working with the SQL language and databases for over two decades, and I've got a real noodle scratcher.
A stored procedure that had been working fine for years suddenly stopped working. The procedure used a series of Insert Into #TempTables that were references further down in the procedure. Again, for years, this worked fine. Then suddenly, the other day, we started getting invalid column errors on the last couple of queries based on temp tables created before it. I know SQL doesn't necessarily run procedurally, but this query ran reliable just fine before now, so something strange is afoot.
Now, if I run each query of the procedure individually in order, I'm able run the entire procedure without error - it was only when the procedure was called from the front end (or when debugging a different procedure that called the offending procedure) that the error would occur.
After a quick search, I concluded that the best solution would be to do away with the temp tables, and simply nest the queries as sub queries in the "From" expression, eliminating the procedural nature of the procedure. Once I did that, the procedure ran fine.
But my question is - why would something that worked fine for ages suddenly stop working? This is what is troubling me, and more importantly, my boss.
There were no updates made to SQL Server around the time the procedure stopped working, so I don't think it's related to that. And while there has been additional data added, I'm still hard-pressed to believe that we broke that camel's back where the procedure suddenly stopped working. (I confirmed this by running the procedure on a restored version of the data from the last time it ran successfully; the procedure still errored out.)
Anyone seen this before?
Thanks for the feedback!

Trace insert of column in SQL Server

My company has a process where data gets moved from one DB to a completely different DB. When a stored procedure gets called, it calls a bunch of other stored procedures and also passes through some triggers. I have a value that is being inserted incorrectly and I'm just trying to find out where the value is being calculated wrong. I tried using Profiler, but have had little success in isolating they problem.
Is there a way to set breakpoints for every step without opening up hundreds of stored procedures and triggers and manually toggling on breakpoints on every line?
Ideally, I'd like to only step through lines that modify a particular variable, but I'll settle for tracing through everything. This is SQL Server 2008 R2.
It sounds like you might benefit from simply executing the stored proc in a debugger. You can inspect variables while the code is being stepped through.
This is probably easiest/slickest to do using Visual Studio. This is the MS article on doing that -- it is pretty simple to do, esp. if you already know how to debug code in VS.

Random timeouts on 1 specific stored procedure from SQL Azure

We have a database hosted in SQL Azure that we connect to through a Cloud Service webapp. Every once a while, one specific stored procedure that returns 100 rows throws a timeout exception when ran from the actual webapp. When we run the same stored procedure with the same parameters from the SQL Management Studio we get the actual results. This issue persists for a while and sometimes disappears as fast as it occurred.
Other stored procedures and data retrieval from our application works like a charm, but one specific SP has this issue, which is weird. When the issue occurs, we can temporarily fix it by adding something like WHERE 1=1 into the where clause. Then it works for a while, but at some point the whole things starts all over again. I cannot get a grip on what's going wrong here or what could be causing this. We also added WITH RECOMPILE to the stored proc, but to no avail.
I experienced the same thing with mine. I checked the execution plan and everything looked fine. So I copied the database to my local machine and it ran there without any problems. Finally I decided to drop and recreate the stored procedure that was timing out. After doing that, the SP ran normally again with no timeouts.
We have the same issue especially after upgrading or downgrading a server. We found that a quick alter including "with recompile" fixes the problem. We turn recompile off after we've altered it once. Not sure if Azure gets some kind of corruption in the execution plan or what is happening. Not convinced you need "with recompile" you may just need to do the alter.

Resources