0

I'm trying to group column values by a specific column using FOR XML PATH('') in SQL Server.

For example when I pass the ModuleID value 1 then output RoleName : Admin Administrator Super Admin which is present is same line.

output

ALTER PROCEDURE [dbo].[MEDEIL_SiteRoleModules_SelectOne] 
    @ModuleID int
AS
    SELECT 
        t1.ModuleID, 
        RoleName = (SELECT s1.RoleName AS [data()] 
                    FROM SiteRoleModules t2 
                    INNER JOIN SiteRoles s1 ON t2.RoleID = s1.RoleID 
                    WHERE t2.ModuleID = t1.ModuleID 
                    GROUP BY s1.RoleName 
                    FOR XML PATH('')) 
    FROM
        SiteRoleModules t1 
    WHERE
        ModuleId = @ModuleID 
    GROUP BY 
        ModuleID

I need to display one by one

Sql Output

5
  • Doesn't make sense to use Where ModuleId = 1 with Group since your code will only return one Group. Remove the Where. Commented Dec 8, 2017 at 5:43
  • ok now check it updated code @jdweng Commented Dec 8, 2017 at 5:44
  • Is it working? Does the database have data for Admin, Administrator, and Super Admin? Commented Dec 8, 2017 at 5:57
  • yes working fine which is present is same line. @jdweng Commented Dec 8, 2017 at 5:58
  • Use String_Agg and make seperator a 0x0A or 0x0D. See : learn.microsoft.com/en-us/sql/t-sql/functions/… Commented Dec 8, 2017 at 6:11

2 Answers 2

1

You can just try to use CHAR(10) to break lines;

SELECT 
    t1.ModuleID, 
    RoleName = (SELECT CHAR(10) + s1.RoleName 
                FROM SiteRoleModules t2 
                INNER JOIN SiteRoles s1 ON t2.RoleID = s1.RoleID 
                WHERE t2.ModuleID = t1.ModuleID 
                GROUP BY s1.RoleName 
                FOR XML PATH('')) 
FROM
    SiteRoleModules t1 
WHERE
    ModuleId = @ModuleID 
GROUP BY 
    ModuleID
Sign up to request clarification or add additional context in comments.

2 Comments

Be careful that, management studio still display it same line but if you copy-paste the cell, you will see that are broken the lines.
I said it made for an example. If you read the cell from your application or from somewhere, the column is got as "Admin\nSuperAdmin" etc.
0

You could try xquery.. .node method

select 
           case when row_number() over (partition by a.ModuleID order by a.ModuleID) > 1 then '' else cast(a.ModuleID as varchar) end [ModuleID],
           id.value('.', 'varchar(50)') [RoleName] from
     (
        select ModuleID ,  cast('<m>'+replace(RoleName, space(1), '</m><m>')+'</m>' as xml) RoleName  from <table>
     ) a cross apply RoleName.nodes ('/m') as RoleName(id)

Result :

ModuleID    RoleName
1           Admin
            Administrator
            Super
            Admin

3 Comments

invalid column name ModuleID and RoleName
@IvinRaj try to change the table name with your with appropriate column name.
can you add table name am new in this technology

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.