Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:11:26+00:00 2026-05-28T04:11:26+00:00

Every now and then we get this error from some of our old ASP

  • 0

Every now and then we get this error from some of our old ASP pages that use a Stored Procedure to return a recordset(usually <2000 rows).

Microsoft OLE DB Provider for ODBC Drivers (0x80040E31)
[Microsoft][ODBC SQL Server Driver]Timeout expired

When I run the SP on the server it runs without an issue but with ASP it just gives a Timeout expired.

When I recompile the SP it fixes the issue and the SP works fine in ASP once more. But then # of days/weeks later I get the same problem either from the same SP/ASP or another. The source tables and ASP are not being changed at all.

It’s got me stumped.

Sample SP:

USE [DB-Name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[MY_StoredProcedure]
    @Filter varchar(50) = null,
    @SortOrder bit = 1,
    @SortField varchar(50) = 'MyCol1',
    @Page int = 1,
    @RecordsPerPage int = 20
AS
BEGIN
    SET NOCOUNT ON
    CREATE TABLE #TempTable(RowID int IDENTITY,Col1 int,Col2 varchar(20),Col3 datetime)
    INSERT INTO #TempTable
    SELECT Col1, Col2, Col3 FROM [DB-Table]
    WHERE 
        @Filter is null OR (
            Col1 LIKE '%' + @Filter + '%'
            OR Col2 LIKE '%' + @Filter + '%'
            OR Col3 LIKE '%' + @Filter + '%'
        )       
    ORDER By
        CASE WHEN @SortField = 'MyCol1' AND @SortOrder = 1 THEN Col1 END ASC,
        CASE WHEN @SortField = 'MyCol1' AND @SortOrder = 0 THEN Col1 END DESC,
        CASE WHEN @SortField = 'MyCol2' AND @SortOrder = 0 THEN Col2 END DESC,
        CASE WHEN @SortField = 'MYCol2' AND @SortOrder = 1 THEN Col2 END ASC,
        CASE WHEN @SortField = 'MyCol3' AND @SortOrder = 1 THEN Col3 END ASC,
        CASE WHEN @SortField = 'MyCol4' AND @SortOrder = 0 THEN Col3 END DESC

    -- Find out the first and last record we want
    DECLARE @FirstRec int, @LastRec int
    SELECT @FirstRec = (@Page - 1) * @RecordsPerPage
    SELECT @LastRec = (@Page * @RecordsPerPage + 1)

    -- Now, return the set of paged records, plus, an indiciation of if we have more     records or not!
    SELECT *,TotalRecords=(SELECT COUNT(RowID) FROM #TempTable),MoreRecords=(SELECT     COUNT(RowID) FROM #TempTable TI WHERE TI.RowID >= @LastRec)
    FROM #TempTable
    WHERE 
         RowID > @FirstRec AND RowID < @LastRec

    DROP TABLE #TempTable
    -- Turn NOCOUNT back OFF
    SET NOCOUNT OFF
END
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:11:27+00:00Added an answer on May 28, 2026 at 4:11 am

    What you have described sounds like an incorrectly cached query plan as a result of parameter sniffing.

    It can sometimes be avoided by ensuring your statistics and indexes are up to date.

    Suggest you post the stored procedure in question.

    The canonical reference is: Slow in the Application, Fast in SSMS?

    In SQL Server 2008 onwards you can make use of OPTIMIZE FOR :

    SELECT Col1, Col2, Col3 FROM [DB-Table] 
    WHERE  
        @Filter is null OR ( 
            Col1 LIKE '%' + @Filter + '%' 
            OR Col2 LIKE '%' + @Filter + '%' 
            OR Col3 LIKE '%' + @Filter + '%' 
        )        
    ORDER By 
        CASE WHEN @SortField = 'MyCol1' AND @SortOrder = 1 THEN Col1 END ASC, 
        CASE WHEN @SortField = 'MyCol1' AND @SortOrder = 0 THEN Col1 END DESC, 
        CASE WHEN @SortField = 'MyCol2' AND @SortOrder = 0 THEN Col2 END DESC, 
        CASE WHEN @SortField = 'MYCol2' AND @SortOrder = 1 THEN Col2 END ASC, 
        CASE WHEN @SortField = 'MyCol3' AND @SortOrder = 1 THEN Col3 END ASC, 
        CASE WHEN @SortField = 'MyCol4' AND @SortOrder = 0 THEN Col3 END DESC 
    OPTION (OPTIMIZE FOR (@Filter UNKNOWN))
    

    This tells SQL Server to eliminate parameter sniffing for parameter @Filter, and use an average selectivity value (based upon statistics).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every now and then (ahem...) my code crashes on some system; quite often, my
Every now and then I receive a Word Document that I have to display
A term that I see every now and then is Cyclomatic Complexity. Here on
I'm finding this problem every now and then in my production website, and it
I work with my own projects and every-now-and-then I do some work for third-parties
Is anyone familiar with this issue, I seem to get it every now and
Every now and then I need to call new[] for built-in types (usually char
Every now and then I have a problem with SVN inside eclipse folder gets
Every now and then I run into a situation when I need to email
Every now and then Elastic Load Balancer (ELB) kicks out one of my servers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.