我有这种格式的表值
sam
jack
sam
john
Declare @name varchar(max)
select @name = COALESCE(@name + ', ','')+ user_email
from PostedCommentMaster where article_id = @id
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得不同的价值
sam,jack,john
Run Code Online (Sandbox Code Playgroud)
像这样.
Lie*_*ers 19
您可以将select语句包装到子选择中并对结果应用合并.
Declare @name varchar(max)
select @name = COALESCE(@name + ', ','') + user_email
from (select distinct user_email
from PostedCommentMaster
where article_id = @id) pc
Run Code Online (Sandbox Code Playgroud)
请注意,这使用SQL Server的未记录功能将结果连接成一个字符串.虽然我找不到它的链接,但我记得读到你不应该依赖这种行为.
更好的选择是使用FOR XML语法返回连接字符串.对SO的搜索会返回您可以用作示例的多个结果.