[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 580: sizeof(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 636: sizeof(): Parameter must be an array or an object that implements Countable
Unofficial UNIT-e Forum • TVF_ActivityStaffFaculties
Page 1 of 1

TVF_ActivityStaffFaculties

Posted: Thu Mar 02, 2017 10:32 pm
by robinwilson16
Returns the faculties a staff member teaches in (used for staff utilisation report)

Code: Select all

CREATE FUNCTION [dbo].[TVF_ActivityStaffFaculties] (
	@Session VARCHAR(10), 
	@StaffCode VARCHAR(20)
)
RETURNS TABLE
AS
RETURN

	--DECLARE @StaffCode VARCHAR(20) = '017518'

	SELECT
		Value = 
			STUFF (
				(
					SELECT DISTINCT
						', ' + FAC.d_reference
					FROM ulive.dbo.capd_staff STF2
					INNER JOIN ulive.dbo.capd_staffactivity STFA
						ON STFA.sa_activitystaff = STF2.s_id
					INNER JOIN ulive.dbo.capd_activity A
						ON A.a_id = STFA.sa_activity
					INNER JOIN ulive.dbo.capd_moduleactivity MA
						ON MA.ma_activity = A.a_id
					INNER JOIN ulive.dbo.capd_module GRP
						ON GRP.m_id = MA.ma_activitymodule
					INNER JOIN ulive.dbo.capd_department FAC
						ON FAC.d_id = GRP.m_moduledept
					WHERE 
						STF2.s_id = STF.s_id
						AND GRP.m_start <= LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31'
						AND GRP.m_end >= LEFT ( @Session, 4 ) + '-08-01'​​
					ORDER BY ', ' + FAC.d_reference FOR XML PATH(''),
					TYPE 
				).value('.','varchar(max)') ,1,2, '' 
			)
	FROM ulive.dbo.capd_staff STF
	WHERE
		STF.s_staffreference = @StaffCode
	GROUP BY
		STF.s_id