CREATE PROCEDURE [dbo].[spGetSettingByName]
	@fullname varchar(max) = '',
	@delimiter char(1) = '|'
AS
BEGIN
	WITH cteAllSettings(parent_id, id, fullname) AS
	(
		SELECT
			t1.id,
			t2.id,
			cast(t2.name as varchar(max))
		FROM tblSettings t1
		JOIN tblSettings t2
		ON	t2.parent_id = t1.id
		UNION ALL
		SELECT
			t1.id,
			t2.id,
			t1.fullname + @delimiter + t2.name
		FROM cteAllSettings t1
		JOIN tblSettings t2
		ON t2.parent_id = t1.id
		WHERE t2.id <> t1.id
	),
	cteSettings(parent_id, id, fullname) AS
	(
		SELECT parent_id, id, fullname
		FROM cteAllSettings
		WHERE fullname = @fullname
		UNION ALL
		SELECT
			t1.id,
			t2.id,
			t1.fullname + @delimiter + t2.name
		FROM cteSettings t1
		JOIN tblSettings t2
		ON t2.parent_id = t1.id
		WHERE t2.id <> t1.id
	)
	SELECT t2.parent_id, t2.id, t2.name, t2.value, t2.enabled, t1.fullname
	FROM cteSettings t1
	JOIN tblSettings t2
	ON	t2.id = t1.id
	ORDER BY parent_id, id
END