CREATE OR ALTER FUNCTION [dbo].[MIG_ANZ_GET_FIELDS_BY_UDTP_TYPE_NAME_Fn]
(
	@BASED_ON VARCHAR(40),
    @TYPE_NAME VARCHAR(40)
)
RETURNS VARCHAR(8000)
AS
BEGIN
	DECLARE @fieldsList varchar(8000)                        

	IF @BASED_ON = 'ZZ_CLASS_ATTR'
		BEGIN
			SELECT 
				@fieldsList = COALESCE(@fieldsList + ' , ', '') + ZZ_CLASS_ATTR.CLASS_ID + ' | ' + ZZ_CLASS_ATTR.ATTR_NAME
			FROM ZZ_CLASS_ATTR WITH (NOLOCK)
			WHERE ZZ_CLASS_ATTR.UDTP_REFERENCE = @TYPE_NAME
			ORDER BY ZZ_CLASS_ATTR.CLASS_ID, ZZ_CLASS_ATTR.ATTR_NAME
		END
	ELSE IF @BASED_ON = 'OBJ_DEF_ATTR'
		BEGIN
			SELECT 
				@fieldsList = 
					COALESCE(@fieldsList + ' , ', '') + OBJ_DEF_ATTR.OBJ_TITLE + ' | ' + OBJ_DEF_ATTR.LINK_TITLE + ' | ' + OBJ_DEF_ATTR.ATTR_NAME
			FROM OBJ_DEF_ATTR WITH (NOLOCK)
			WHERE OBJ_DEF_ATTR.CLASS_REFERENCE = 'ZZ_UDCDTYPE'
			  AND OBJ_DEF_ATTR.ATTR_REFERENCE = @TYPE_NAME
			ORDER BY OBJ_DEF_ATTR.OBJ_TITLE, OBJ_DEF_ATTR.LINK_TITLE, OBJ_DEF_ATTR.ATTR_NAME
		END

	SET @fieldsList = ISNULL(@fieldsList, '')

    RETURN @fieldsList
END
