TVF_ActivityStaffFaculties

The list of table valued functions used by Guildford College Reports and any anyone else wishes to share
Post Reply
User avatar
robinwilson16
Site Admin
Posts: 184
Joined: Sun Oct 16, 2016 6:46 pm
Forename: Robin
Surname: Wilson
College Name: FEA
Position: MIS Consultant
LinkedIn Profile: https://www.linkedin.com/in/robinwilson16

TVF_ActivityStaffFaculties

Post by robinwilson16 » Thu Mar 02, 2017 10:32 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest