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 795485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:29:16+00:00 2026-05-14T22:29:16+00:00

The requirement says: stored procedure meant to search data, based on 5 identifiers. If

  • 0

The requirement says: stored procedure meant to search data, based on 5 identifiers. If there is an exact match return ONLY the exact match, if not but there is an exact match on the not null parameters return ONLY these results, otherwise return any match on any 4 not null parameters… and so on

My (simplified) code looks like:

create procedure xxxSearch @a nvarchar(80), @b nvarchar(80)...
as 
begin
  select whatever 
    from MyTable t
    where ((@a is null and t.a is null) or (@a = t.a)) and
          ((@b is null and t.b is null) or (@b = t.b))...

    if @@ROWCOUNT = 0
    begin
        select whatever 
          from MyTable t
          where ((@a is null) or (@a = t.a)) and
                ((@b is null) or (@b = t.b))...
          if @@ROWCOUNT = 0
          begin
             ...
          end
    end
end

As a result there can be more sets of results selected, the first ones empty and I only need the last one.
I know that it is easy to get the only the last result set on the application side, but all our stored procedure calls go through a framework that expects the significant results in the first table and I’m not eager to change it and test all the existing SPs.

Is there a way to return only the last select results from a stored procedure?
Is there a better way to do this task ?

  • 1 1 Answer
  • 1 View
  • 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-14T22:29:17+00:00Added an answer on May 14, 2026 at 10:29 pm

    Use a table variable:

    create procedure xxxSearch @a nvarchar(80), @b nvarchar(80)...
    as 
    begin
      DECLARE @res TABLE(...)
      INSERT INTO @res(...)
      select whatever 
        from MyTable t
        where ((@a is null and t.a is null) or (@a = t.a)) and
              ((@b is null and t.b is null) or (@b = t.b))...
    
        if @@ROWCOUNT = 0
        begin
            INSERT INTO @res(...)
            select whatever 
              from MyTable t
              where ((@a is null) or (@a = t.a)) and
                    ((@b is null) or (@b = t.b))...
              if @@ROWCOUNT = 0
              begin
                 ...
              end
        end
        SELECT ... FROM @res
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement, which says, If file exists and is not readable then
I have a requirement which says I have to create only once class file
My requirement is to create an Enum based on values present in a table
Consider the following code (from a requirement that says that 3 is special for
On page 42 of Code Code complete there's a checklist of requirement items you
I've done a search on this subject already, and have found the same data
I think this example says it all . The requirement is to show recent
Regarding to the requirement for C++ stl container element, the standard says: the element
The docs says i could only execute the crawl command inside the project dir
The business requirement says that the PDF cannot be printed. I looked up the

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.