Link to home
Start Free TrialLog in
Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Quick one - quickly setting a bit value based on exists statement

I'm trying to streamline my proc's and reduce the amount of code I have.

Given the following code that sets a bit value, is it possible to set the value of the bit variable all in one line, instead of the method used below?

            DECLARE @bitValue bit
            IF EXISTS (SELECT * FROM @someTableVar t1 INNER JOIN @anotherTableVar t2 ON t1.ID = t2.ID)
                  BEGIN
                        SET @bitValue = 1
                  END
            ELSE
                  BEGIN
                        SET @bitValue = 0
                  END
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joechina
joechina

How about

SELECT @bitValue = (SELECT count(1) FROM @someTableVar t1 INNER JOIN @anotherTableVar t2 ON t1.ID = t2.ID)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rouchie

ASKER

Thanks guys!