TVF_RegisterRooms

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: 185
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_RegisterRooms

Post by robinwilson16 » Thu Mar 02, 2017 11:47 pm

Returns all the rooms attached to a register as a single comma separated list

Code: Select all

CREATE FUNCTION [dbo].[TVF_RegisterRooms] (
	@RegisterID numeric(16, 0)
)
RETURNS TABLE
AS
RETURN

SELECT
	Value = 
		STUFF (
			(
				SELECT DISTINCT
					', ' + RM.r_reference
				FROM ulive.dbo.capd_roomregister RREG2
				INNER JOIN ulive.dbo.capd_room RM
					ON RM.r_id = RREG2.rr_room
				WHERE 
					RREG2.rr_register = RREG.rr_register
				ORDER BY ', ' + RM.r_reference FOR XML PATH(''),
				TYPE 
			).value('.','varchar(max)') ,1,2, '' 
		)
FROM ulive.dbo.capd_roomregister RREG
INNER JOIN ulive.dbo.capd_room RM
	ON RM.r_id = RREG.rr_room
WHERE
	RREG.rr_register = @RegisterID
GROUP BY
	RREG.rr_register

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests