Strange problem with SQL Server 2005 Stored procedure performing the same SELECT many, many times
I have a weird problem. Let me give you some information.
There is a stored procedure that has a single selection that returns a dataset and a return value of 0.
Before the "main" selection in sp, also the fake selection (due to SSIS and codegen tools) looks like this:
IF (1=0)
BEGIN
CONVERT(INT, NULL) AS Id
,CONVERT(VARCHAR(50), NULL) AS someColumn
,...
END
There's one latest release from the usual - the basic selection construct uses the "TOP" keyword with an integer variable instead of a scalar value. Looks like:
SELECT TOP (@top)
...
...
FROM
...
Now before the problem: when the procedure is called from SSMS directly, it runs within a few ms. When called from an application, it starts up after a few minutes. We discovered through SQL Profiler that the "main" selection within a procedure is called thousands of times when the procedure is called from within an application (we are using the MS Enterprise SqlDatabase object library, ExecuteReader method, .NET 2.0 approach).
The point is that our application calls hundreds of other procedures without any similar problems for many years. So, I hope that someone has the knowledge that we are missing.
a source to share