2

I have:

DECLARE @Authenticated BIT

If @@ROWCOUNT > 0
 begin
   SELECT @Authenticated = 1
 end ELSE
 begin
   SELECT @Authenticated = 0
 end 

Is there a way I could do this without the BEGIN END and also is setting a BIT datatype a good way to represent True of False?

3 Answers 3

6

You could use CASE to do this more concisely, like so:

 SET @Authenticated = (SELECT CASE WHEN @@ROWCOUNT >0 THEN 1 ELSE 0 END)

Also, BIT would be a good choice to represent True/False, since that is pretty much what bits work like - either 0 or 1.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I will accept this answer in 4 minutes. Can I also ask. Is using SET a better way to set a value. My code is using: SELECT @Result = 'F'. Should I be using SET for this instead?
This answer gives a good comparison between the 2: stackoverflow.com/questions/866767/…
1

You can change your IF .. ELSE construct a bit like below and use SET command

DECLARE @Authenticated BIT

If @@ROWCOUNT > 0
SET @Authenticated = 1
ELSE
SET @Authenticated = 0

Comments

1
DECLARE @Authenticated BIT
SET @Authenticated = @@ROWCOUNT

should also work since BIT values are true/1 if not zero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.