ALTER FUNCTION getDictHierarchy ( @childValue varchar(50), @dictCode varchar(30))
RETURNS varchar(100) AS BEGIN -- Declare the return variable here DECLARE varchar(1000)set = ''
;WITH cte AS (
SELECT DM_ID, DM_PARENTID, DM_NAME, NODE_LEVEL FROM CODE_COMMON WHERE DM_VALUE = @childValue and DM_CODE = @dictCode UNION ALL SELECT t.DM_ID, t.DM_PARENTID, t.DM_NAME, t.NODE_LEVEL FROM CODE_COMMON AS t INNER JOIN cte AS c ON t.DM_ID = c.DM_PARENTID ) select @result = '' + (select '-' + cte.DM_NAME FROM cte ORDER BY NODE_LEVEL FOR XML path ( '' )) set @result = RIGHT (@result,LEN(@result ) - 1)RETURN @result
END