简体   繁体   中英

How to create a stored procedure to select multiple data and display in one column in SQL Server

When is the ID is all selected for below table, the result should display as below result

+-----------+
| ID | Name |
+-----------+
| 1  |a     |
| 2  |b     |
| 3  |c     |
+-----------+

Result:

Name
-----
a b c

please try below:

SELECT Name + ' ' as Name
FROM table_name
FOR XML PATH ('')

Hope it solves your problem!

If you are using SQL Server 2017 onwards. String_Agg

SELECT STRING_AGG(Name,' ') AS Name
FROM table_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM