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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:25:17+00:00 2026-05-28T07:25:17+00:00

How should i go about doing something like: IF the following select has any

  • 0

How should i go about doing something like:

IF the following select has any rows

SELECT ID, NAME FROM TABLE WHERE ID=@ID

then return that actual select, otherwise use another select statement.

I use somwthing like

IF EXISTS(SELECT ID, NAME FROM TABLE WHERE ID=@ID)
    SELECT ID, NAME FROM TABLE WHERE ID=@ID
ELSE
    SELECT QTY FROM TABLE WHERE ID=@ID

but this way i use the first select twice(once in the check and once the actual return record set.

  • 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-28T07:25:17+00:00Added an answer on May 28, 2026 at 7:25 am

    It’s not as inefficient as you think. The EXISTS keyword means that the optimiser stops after 1 record is found.

    But, if you really want to avoid repetition, you can do something like…

    SELECT ID, NAME FROM TABLE WHERE ID=@ID
    
    IF (@@rowcount = 0)
        SELECT QTY FROM TABLE WHERE ID=@ID
    

    The problem here is that you now return up to 2 record sets to the client. The first of which my be empty (but field headers, etc, are still returned – just with 0 records).

    You could avoid that by inserting results into a table variable, and only returning them to the client if there are any records. But I think it would be noticeably slower than just using EXISTS() like you already do.

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

Sidebar

Related Questions

Are there any open-source libraries that all programmers should know about? I'm thinking something
I am having trouble doing something simple like the following using (SPSite site =
Are there any VC++ settings I should know about to generate better PDB files
Are there any big differences a developer should care about?
I have something like the following: var myTimeout; function delayStuffUntil( when ){ if( myTimeout)
Let's say I have an XML node like the following: <item id=20> <name>Baz</name> <description>
For some reason I'm running a blank on how to go about doing something
I current test uniqueness validations in rspec using something like the following: @unique =
Suppose that one has some lock based code like the following where mutexes are
The following is an excerpt from some code that I'm experimenting with: has buffer

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.