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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:49:19+00:00 2026-06-04T17:49:19+00:00

I am doing a password authentication query like this. if exists(select UserID from users

  • 0

I am doing a password authentication query like this.

if exists(select UserID from users where UserName=@UserName and Password=@Password) 
    select UserID from users where UserName=@UserName and Password=@Password
else 
    select 0

but it seems to me that I am querying twice for a single result
Can I just do this

Select UserID Where Username = @UserName and Password = @Password

and when I get my results just check the reader for number of results?

if (!myReader.HasRows)
    MessageBox.Show("UserName not found or Password invalid");
else
    //do login stuff

Guess what I am asking is, which of these is better, or are they the same?

  • 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-04T17:49:20+00:00Added an answer on June 4, 2026 at 5:49 pm

    Use this:

    SELECT  ISNULL
            (
            (
            SELECT  userId
            FROM    users
            WHERE   userName = @userName
                    AND password = @password
            ),
            0
            )
    

    Your solution with checking number of records would work too of course.

    Note that it will always return exactly one record and will fail if userName is not unique and there are duplicates of @userName.

    As noted by @Andriy M, COALESCE will not help here since it’s being internally rewritten into a CASE prone to the same problem.

    Answering your question: yes, the original batch would access users twice.

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

Sidebar

Related Questions

I'm doing a Thunderbird extension that will get the stored username/password in Firefox and
I am crawling a page that requires username and password for authentication. And I
I've never seen this done but I had an idea of doing authorization in
I want to use transport security mode with username/password authentication and ssl, it works
I'm designing an authentication system that works like the following: User enters password Salt
I am writing a WCF Service that will require a username and password authentication,
I'm trying to build [Ryan Bates' authenticaion from scratch][1] using sinatra but authentication does
I'm doing password based file encryption in Java; I'm using AES as the underlying
Doing a mysqli query and COUNTing the results, is there a preferred comparison operator
I've recently started tinkering with ASP.NET MVC, but this question should apply to classic

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.