0

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.

DECLARE @maybe bit

IF exists( SELECT * FROM UserList WHERE UserName = 'Tester')
	SET @maybe = 1
ELSE 
	SET @maybe = 0

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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by HBMSGuy
0

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

select cast(case when exists (SELECT * FROM UserList WHERE UserName = 'Tester') then 1 else 0 end as bit)
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.