3 tables:tblDepartmentsDepartmentIDDepartmentDescription(there is a department in here called 'global')tblHolidaysHolidayIDHolidayDateHolidayDescriptiontblDepartmentHolidaysDepartmentHolidayIDDepartmentID (a DepartmentID of zero means it is 'global' - i.e. applies to all departments)HolidayIDIdea is that different departments have different 'holidays' - they all share public holidays like Christmas .. but different departments are not available on specific days - attending seminars, training etc.I want to return data that will look like this:Holiday Description Holiday Date Global Dept1 Dept2 Dept3 Dept4Christmas Day 25/12/2009 Yes Yes Yes Yes YesSeminar 2 16/06/2010 No No Yes No Yes (Sorry, can't get that to layout in columns.) But you get the idea, each row contains data about a specific day and whether each department has that day off or notThe stored procedure I have to return the data looks like this:DECLARE @GlobalDeptID intSET @GlobalDeptID = (SELECT DepartmentID FROM tblDepartments WHERE DepartmentDescription = 'GlobalDepartment')DECLARE @Dept1ID intSET @Dept1ID = (SELECT DepartmentID FROM tblDepartments WHERE DepartmentDescription = 'Department 1')DECLARE @Dept2ID intSET @Dept2ID = (SELECT DepartmentID FROM tblDepartments WHERE DepartmentDescription = 'Department 2')DECLARE @Dept3ID intSET @Dept3ID = (SELECT DepartmentID FROM tblDepartments WHERE DepartmentDescription = 'Department 3')DECLARE @Dept4ID intSET @Dept4ID = (SELECT DepartmentID FROM tblDepartments WHERE DepartmentDescription = 'Department 4')SELECT DISTINCT tbhHolidays.HolidayDescription, CONVERT(VARCHAR(10), tbhHolidays.HolidayDate, 103) AS [Holiday Date], CASE WHEN @GlobalDeptID IN (SELECT DepartmentID FROM tblBankHolidaysWPID WHERE tblBankHolidaysWPID.BHID = tblBankHolidays.BHID) THEN 'true' ELSE 'false' END AS 'Global', CASE WHEN @Dept1ID IN (SELECT DepartmentID FROM tblDepartmentHolidays WHERE tblDepartmentHolidays.HolidayID = tblHolidays.HolidayID) THEN 'true' ELSE 'false' END AS 'Dept1', CASE WHEN @Dept2ID IN (SELECT DepartmentID FROM tblDepartmentHolidays WHERE tblDepartmentHolidays.HolidayID = tblHolidays.HolidayID) THEN 'true' ELSE 'false' END AS 'Dept2', CASE WHEN @Dept3ID IN (SELECT DepartmentID FROM tblDepartmentHolidays WHERE tblDepartmentHolidays.HolidayID = tblHolidays.HolidayID) THEN 'true' ELSE 'false' END AS 'Dept3', CASE WHEN @Dept4ID IN (SELECT DepartmentID FROM tblDepartmentHolidays WHERE tblDepartmentHolidays.HolidayID = tblHolidays.HolidayID) THEN 'true' ELSE 'false' END AS 'Dept4' FROM tblDepartmentHolidays INNER JOIN tblHolidays ON tblDepartmentHolidays.HolidayID = tblHolidays.HolidayID WHERE tblBankHolidays.HolidayDate >= (SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) ORDER BY tblHolidays.HolidayDate
Now, this all works - but it is effectively hard coded. What happens when someone adds Department 5 - or deletes Department 3? I have to come back into the stored procedure and alter it.I'm hoping to be able to write this in such a way that the departments are retrieved from the departments table - and then 'used' - rather than manually getting the DepartmentID of each department and testing to see if this is in the tblDepartmentHolidays table - as I go through the table.Thanks for any help.