I have a table. I have 2 variables, one is a bit, the other is an int.
Table: WorkGroupCollectionDetail
Variables: @WorkgroupID int, @IsFSBP bit
The table has WorkGroupId int PK and WorkGroupCollectionCode varchar PK. That’s it.
I can run a query like this:
SELECT WorkGroupId
FROM WorkGroupCollectionDetail
WHERE WorkGroupCollectionCode = 'FSBP'
and it gives me a list of WorkGroupID.
So what I need to do is if the value of @WorkgroupID is inside the results of that query, I need to set the bit variable to true.
which is logically equivalent to:
A query using
EXISTSoften performs better than a query usingIN. You can check the execution plans to see how they compare in your particular case.Note that these examples include setting the bit value to zero as well as one.