Query database to see if value already exists one statment

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 26
Reputation: HBMSGuy is an unknown quantity at this point 
Solved Threads: 1
HBMSGuy HBMSGuy is offline Offline
Light Poster

Query database to see if value already exists one statment

 
0
  #1
Jul 28th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster

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

 
0
  #2
Jul 29th, 2009
maybe you can use CASE ... WHEN ... END command like below:

  1. SELECT cast(case when EXISTS (SELECT * FROM UserList WHERE UserName = 'Tester') then 1 else 0 end AS bit)
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 26
Reputation: HBMSGuy is an unknown quantity at this point 
Solved Threads: 1
HBMSGuy HBMSGuy is offline Offline
Light Poster

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

 
0
  #3
Jul 29th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC