In the following SQL, @UserExtendedSecurity
is a table variable which simply provides a 1 or 0 indicator based on whether or not a user has extended security:
INSERT INTO @UserExtendedSecurity (UserId, UserName, HasExtendedSecurity)
SELECT
ue.Id,
adu.FirstName + ' ' + adu.LastName,
0
FROM
UserExtension ue
JOIN
ADUser adu ON ue.ADUserId = adu.Id
WHERE
ue.Id NOT IN (SELECT UserId FROM @UserExtendedSecurity)
In the SQL example above, @UserExtendedSecurity
already has all users with HasExtendedSecurity = 1
, so I'm just selecting the remaining users into the table var with a HasExtendedSecurity
value of 0. The NOT IN
clause at the end does what it needs to do but is there a more proper/elegant/efficient approach to accomplish what I'm doing? Do SQL experts consider using NOT IN
in this way as a red flag?