CREATE FUNCTION [dbo].[ufn_Admin_GetSubGroupByGroupId](@parentId AS INT) RETURNS TABLE
AS
RETURN
WITH SubsCTE
AS
(
-- Anchor member returns a row for the input manager
SELECT GroupId
FROM dbo.tb_Group
WHERE GroupId = @parentId
UNION ALL
-- Recursive member returns next level of subordinates
SELECT C.GroupId
FROM SubsCTE AS P
JOIN dbo.tb_Group AS C
ON C.Parent = P.GroupId
)
SELECT * FROM SubsCTE;
select * from [dbo].[ufn_Admin_GetSubGroupByGroupId](@parentId)
CREATE FUNCTION [dbo].[ufn_SplitStr](@SourceSql VARCHAR(8000), @StrSeprate VARCHAR(100))
RETURNS @temp TABLE(UniqueId INT IDENTITY(1, 1), Id VARCHAR(100))
AS
BEGIN
DECLARE @ch AS VARCHAR(100)
WHILE(@SourceSql <> '')
BEGIN
IF charindex(@StrSeprate, @SourceSql, 1) > 0
BEGIN
SET @ch=left(@SourceSql, charindex(@StrSeprate, @SourceSql, 1) - 1)
IF @ch <> ''
BEGIN
INSERT @temp VALUES( @ch )
END
SET @SourceSql = stuff(@SourceSql, 1, charindex(@StrSeprate, @SourceSql,
1), '')
END
ELSE
BEGIN
INSERT @temp VALUES( @SourceSql )
BREAK
END
END
RETURN
END