Get output in comma separated format in sql server When I retrieve the data I get result in congested format.

TableName       TableRows       DataSpaceMB     IndexSizeMB
Sec             2               0.02            0.02
Key             2               0.02            0.01
name            3               0.01            0.02
agenda          1               0.01            0.05
Configuration   19              0.01            0.02
Policies        2               0.01            0.01


How to get result in separated values for different columns in a single row?

E.g
    TableName,TableRows,DataSpaceMB,IndexSizeMB
    Sec,2,0.02,0.02
    Key,2,0.02,0.01
    name,3,0.01,0.02
    agenda,1,0.01,0.05
    Configuration,19,0.01,0.02
    Policies,2,0.01,0.01
You can use + or the CONCAT function to combine multiple columns into a single string. If you need the column names at the top you can do that with UNION ALL.

Here is your sample data:

CREATE TABLE #X_164768 (
TableName SYSNAME NOT NULL,
TableRows BIGINT NOT NULL,
DataSpaceMB NUMERIC(10,2) NOT NULL,
IndexSizeMB NUMERIC(10,2) NOT NULL
);

INSERT INTO #X_164768
VALUES
('Sec', 2, 0.02, 0.02),
('Key',2, 0.02, 0.01),
('name', 3, 0.01, 0.02),
('agenda', 1, 0.01, 0.05),
('Configuration', 19, 0.01, 0.02),
('Policies', 2, 0.01, 0.01);


Here is one way to get the output you want:

SELECT 'TableName,TableRows,DataSpaceMB,IndexSizeMB'

UNION ALL

SELECT 
Tablename 
+ ',' + CAST(TableRows AS VARCHAR(20))
+ ',' + CAST(DataSpaceMB AS VARCHAR(20))
+ ',' + CAST(IndexSizeMB AS VARCHAR(20))
FROM #X_164768;