我在Sql上有一张表:

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

我想要的是一个函数thar返回带有字符串数据的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)

10-08 16:06