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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:08:58+00:00 2026-06-05T14:08:58+00:00

I just set an SQL Stored Procedure to return a specific row in a

  • 0

I just set an SQL Stored Procedure to return a specific row in a specific order (from more to less important):

-- 1st Level -> Query a more detailed object...
SELECT ... WHERE something
-- if a result is find, the SP is returning the correct row.
-- if rowcount = 0 then, the SP is returning an empty row 
-- and continues with the next query (less specific than the 1st one).

IF @@ROWCOUNT = 0
BEGIN
SELECT .... WHERE something else... 
 -- Again, if a result exists, the SP returns the correct row, but also 
 -- the result of the 1st query is returned without rows...
 IF @@ROWCOUNT = 0
 BEGIN
 SELECT .... WHERE something else again...
 END
END

Is there any option to only return the SELECT statement that returns a non empty row? I don’t want to return empty rows… and each time the row is an “sub-level” of the 1st query, I get empty results before the correct row.

I was thinking to create a table variable. Are there any better ways?

  • 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-05T14:08:59+00:00Added an answer on June 5, 2026 at 2:08 pm

    Since you are looking for the lack of rows why not use an if/else statement in your procedure. Something like:

    If exists(/*firstQuery*/ SELECT ... WHERE something) begin
       SELECT ... WHERE something
    end else if exists (/*secondQuery*/ SELECT .... WHERE something else... ) begin
       SELECT .... WHERE something else... 
    end else /*Third query*/ begin
        SELECT .... WHERE something else again...
    end
    

    Just remember when checking is something exists you should select only one column (for example the id) as if you have calculated columns then this can slow down your procedure.

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

Sidebar

Related Questions

I use the following stored procedure from my SQL Server 2008 database to return
I am having difficulty executing a MS SQL Server stored procedure from Java/jsp. I
This stored procedure doesn't work. It just returns 0 rows. I've checked the SQL
I'm using the UPDLOCK and READPAST sql hints in a stored procedure in order
Could I use migrator.net bare migration framework and just have a set of SQL
I have a SQL Server 2008 stored procedure that updates values in a table.
I'm having a never-ending problem with trying to call a stored procedure from a
One thing that I've always hated more than just about anything in MS SQL
I have a stored procedure that returns a valueI call it from other stored
I'm trying to update a table via a stored procedure from asp.net. I get

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.