943,659 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 1636
  • MS SQL RSS
Jul 28th, 2009
0

Query database to see if value already exists one statment

Expand Post »
Hello,
While working on setting up user accounts, I wanted to see if the values already existed in a database to alert the user before submitting the data. While doing this, I came to an issue. The issue is when I went the MS SQL SMS and tried writing a test query, I couldn't figure out how to write it in one select statment.

MS SQL Syntax (Toggle Plain Text)
  1. DECLARE @maybe bit
  2.  
  3. IF EXISTS( SELECT * FROM UserList WHERE UserName = 'Tester')
  4. SET @maybe = 1
  5. ELSE
  6. SET @maybe = 0
  7.  
  8. SELECT @maybe

Now the issue comes in when I try to move this code over to C#. I am trying to do this with out creating stored procedures because I have many different tables and columns and would prefer writing the code on the System.Data.SqlClient.SqlCommand. Or the best solution is writing a stored procedure where could I make the "FROM Table" and "WHERE Column" parameters passed in from C#

Thanks for all the help,
Johnny
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
HBMSGuy is offline Offline
22 posts
since Apr 2009
Jul 29th, 2009
0

Re: Query database to see if value already exists one statment

maybe you can use CASE ... WHEN ... END command like below:

MS SQL Syntax (Toggle Plain Text)
  1. SELECT cast(case when EXISTS (SELECT * FROM UserList WHERE UserName = 'Tester') then 1 else 0 end AS bit)
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008
Jul 29th, 2009
0

Re: Query database to see if value already exists one statment

Thanks so much. I have been coming up with some weird ideas about how to work this. This is very helpful, and it executes faster than the if...else one I had when I tested it

Thanks,
Johnny
Reputation Points: 10
Solved Threads: 1
Newbie Poster
HBMSGuy is offline Offline
22 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: HELP!!! Converting Hexadecimal to Decimal or vice versa
Next Thread in MS SQL Forum Timeline: Problem restoring Database after Backup





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC