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

Recommended Answers

All 2 Replies

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)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.