En mulighed ville være at oprette en visning fra alle project_xxx-tabellerne. Noget som:
CREATE VIEW SecurityTable
AS
SELECT 'Project_1', User, HasAccess
FROM Project_1
UNION
SELECT 'Project_2', User, HasAccess
FROM Project_2
UNION
SELECT 'Project_3', User, HasAccess
FROM Project_3
etc...
Så kan du blot forespørge på dit syn, som om databasen var blevet designet korrekt i første omgang;-)
SELECT ProjectID, User
FROM SecurityTable
WHERE [criteria]
AND HasAccess=1
Da du angiver, at du ofte vil tilføje nye projekttabeller, vil jeg foreslå, at du kan have en sikkerhedstabel udfyldt med en dynamisk forespørgsel hver morgen. For eksempel:
CREATE TABLE Project_1 (
Usr varchar(20),
HasAccess bit)
GO
CREATE TABLE Project_2 (
Usr varchar(20),
HasAccess bit)
GO
CREATE TABLE SecurityTable (
Usr varchar(20),
HasAccess bit)
GO
INSERT INTO Project_1 (Usr, HasAccess) VALUES ('Kermit', 1)
INSERT INTO Project_1 (Usr, HasAccess) VALUES ('MissPiggy', 1)
INSERT INTO Project_2 (Usr, HasAccess) VALUES ('Beaker', 1)
INSERT INTO Project_2 (Usr, HasAccess) VALUES ('TheCount', 0)
GO
Create Procedure LoadSecurityTable
AS
DELETE * FROM SecurityTable
EXEC sp_MSForEachTable
@command1 = 'INSERT INTO SecurityTable (Usr, HasAccess) SELECT Usr, HasAccess FROM ?',
@whereand = 'AND o.name LIKE ''Project_%'''
GO
EXEC LoadSecurityTable
SELECT * FROM SecurityTable