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

  • SEARCH
  • Home
  • 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 7717467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:54:23+00:00 2026-06-01T02:54:23+00:00

I’m using SQL Server 2008. I have an interesting scenario where a stored procedure

  • 0

I’m using SQL Server 2008.

I have an interesting scenario where a stored procedure (written by a “power user”) has an okay runtime of (around 4 seconds) if there’s data in the primary table. If the search value doesn’t exist, the run time averages out at about 3 minutes. Because of how the process works, and the web application that uses the procedure, it requires an empty result set in the case of no data.

I’ve tested the logic below with values that have data and values that don’t and the flow seems to work; however, when I put my actual query in the else statement, it seems like that part is always being evaluated despite my knowing that logic branch shouldn’t execute.

DECLARE @spId int

SELECT @spId = td.mainId
FROM dbo.PRIMARYTABLE
WHERE td.longId = @searchVal

IF @spId < 1 OR @spId IS NULL
BEGIN
     select 'RETURN EMPTY RESULT SET' as test
END
ELSE 
BEGIN
    SELECT 'DO ACTUAL QUERY' as test
END

When I test this with a dummy value, such as 1111, the select ‘RETURN EMPTY RESULT SET’ as test is returned. When I use a value that I know exists, the SELECT ‘DO ACTUAL QUERY’ as test is returned. When I replace “SELECT ‘DO ACTUAL QUERY’ as test” with the actual heavy duty query and use the same non-existent dummy value, it still looks like the ELSE clause is reached.

What am I missing here?

  • 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-06-01T02:54:25+00:00Added an answer on June 1, 2026 at 2:54 am

    Perhaps you are not showing everything. There is an counter-intuitive thing about assignment in select where no rows are returned – the value of variable will not be cleared. Paste this in SSMS:

    declare @searchVal as int
    set @searchVal=111
    
    DECLARE @spId int
    set @spId = 2134
    SELECT @spId = td.mainId
    FROM (select 839 as mainId, 0 as longid) td
    where td.longId = @searchVal
    print @spid
    

    @spid will be 2134. This is why you should always test using @@rowcount, in you case

    IF @@rowcount = 0 or @spId < 1 or @spId is null
    BEGIN
        select 'RETURN EMPTY RESULT SET' as test
    END
    ELSE 
    BEGIN
        SELECT 'DO ACTUAL QUERY' as test
    END
    

    There is also a possibility of duplicated data by longId, returning random mainid from rows that satisfy @searchval condition.

    Other than that, I would not know.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.