sql-server - 在 SQL 上将行转换为字符串

标签 sql-server sql-server-2008 sql-server-2008-r2

我在 Sql 上有一个表:

ID    User       Observation
========================================
1     John       This is correct!
----------------------------------------
2     Michael    I got an error!
----------------------------------------
3     Joshua     This is incorrect!
----------------------------------------

我想要的是一个函数,它返回一个带有字符串数据的varchar

像这样:

编辑: 这是我期望的结果:

John says: This is correct!\r\nMichael says: I got an error!\r\nJoshua says: This is incorrect

有办法吗?

最佳答案

作为单列和单行你可以这样做

DECLARE @out as varchar(max)

SET @Out = ''

SELECT @Out = @Out +  [User] + ' says: ' + Observation + CHAR(13) + CHAR(10)  
FROM Table1

SELECT @out

See it here

这是 SSMS 中的输出(使用文本输出)

-------------------------------
John says: This is correct!
Michael says: I got an error!
Joshua says: This is incorrect!

(1 row(s) affected)

关于sql-server - 在 SQL 上将行转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10235809/

相关文章:

sql - SQL如果可能,将值转换为int,如果不可能,则将值设置为0

c# - 如何检索 ADO.NET SqlCommand 的结果?

c# - C# 中的 SQL 查询错误 : Using SELECT JOIN WHERE OR

sql-server-2008 - SQL 条件选择案例逻辑

sql-server - SQL Server 查询 XML xpath 空命名空间

c# - 是否可以用 C# 模拟 ms-access 的功能?

sql-server - 执行 sp_send_dbmail 命令时出现“配置文件名称无效”错误

SQL Server 2008 插入时出现 "IDENTITY_INSERT is ON"错误

sql-server - 如何让 MS LightSwitch 识别我的 View ?

sql - 创建全文索引错误