I have 3 tables.
1. Users 4 Cols
UserID - User - RealName - Flags
2. UsersGroups 2 Cols
UserID - GroupID
3. Groups 3 Cols
GroupID - Group - Flags
and I want to set the flags on the User
table for User = 'Administrator'
and apply the same to the Group
table.
I have the following SQL that works, but I also have several flags that I have to apply using bitwise operators.
I've found my code to be really repetitive, so was wondering if anyone could suggest some refactoring that would not affect the performance.
Code:
--- SET FLAG 1
UPDATE User
SET User.Flags = User.Flags | 2048
WHERE User.Value = 'Administrator'
UPDATE dbo.Group
SET dbo.Group.Flags =
(Select e.Flags FROM dbo.User p INNER JOIN dbo.UserInGroup pe ON p.UserID = pe.UserID
INNER JOIN dbo.Group e ON e.GroupID = pe.GroupID
WHERE p.Value = 'Administrator') | 2048
FROM dbo.User p INNER JOIN dbo.UserInGroup pe ON p.UserID = pe.UserID
INNER JOIN dbo.Group e ON e.GroupID = pe.GroupID
WHERE p.Value = 'Administrator'
-- SET FLAG 2
UPDATE User
SET User.Flags = User.Flags | 512
WHERE User.Value = 'Administrator'
UPDATE dbo.Group
SET dbo.Group.Flags =
(Select e.Flags FROM dbo.User p INNER JOIN dbo.UserInGroup pe ON p.UserID = pe.UserID
INNER JOIN dbo.Group e ON e.GroupID = pe.GroupID
WHERE p.Value = 'Administrator') | 512
FROM dbo.User p INNER JOIN dbo.UserInGroup pe ON p.UserID = pe.UserID
INNER JOIN dbo.Group e ON e.GroupID = pe.GroupID
WHERE p.Value = 'Administrator'