I have a table Templates that has Type, ID (pk) and Name columns. These templates can be applied to two different tables, Person and Role. Users can create their own templates, but these will always be of a pre-defined type.
I have written code that checks the usage levels of currently existing templates, however adding in functionality to check for templates that may be created in the future is beyond my current capability.
SELECT t.Name As [Template Name],
CASE
WHEN t.Type = 5 THEN SUM(CASE WHEN p.tID BETWEEN 2 AND 3 THEN 1 ELSE 0 END)
WHEN t.TYPE = 10 THEN SUM(CASE WHEN r.tID BETWEEN 4 AND 5 THEN 1 ELSE 0 END)
WHEN t.TYPE = 100 THEN SUM(CASE WHEN p.tID = 8 THEN 1 ELSE 0 END)
WHEN t.TYPE = 20 THEN SUM(CASE WHEN r.tID BETWEEN 6 AND 7 THEN 1 ELSE 0 END)
WHEN t.TYPE = 3 THEN SUM(CASE WHEN r.tID BETWEEN 9 AND 10 THEN 1 ELSE 0 END)
END As [Times Used],
t.ID As [Template ID], t.Type As [Template Type]
FROM Templates t, Person p, Role r
GROUP BY t.Name, t.ID, t,Type
ORDER BY [Times Used] ASC, t.Type, t.Name, t.ID
...The five types above are constant. As above, I would like the code to be able to check for additional templates that may be added in the future but am not sure how to do this.
As always, any help gratefully received.
Am using SQL Server 2012.
M
p.s. Just as a quick note to everyone here, I inherited this database I have no control over the existing data structure. I am trying to future-proof this as best I can for others.
To clarify, the Template.Type values will never change. However a user could create a new Template (templates can only be applied to a Person or a Role, never both) at any point in the future, which would be of one of the above five types.
For Template.Type, values 5 & 100 indicate that the Template is for a Person. Values 10, 20 & 3 indicate that it is for a Role. As such in those instances only the Person and Role tables respectively will need to be searched through.
My code currently searches for all existing Template.ID values, but I am not sure how to add in functionality to search for ID values that may be created in the future.