将 SQL Server 结果集转换为字符串 [英] Convert SQL Server result set into string

查看:109
本文介绍了将 SQL Server 结果集转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 中得到的结果为

SELECT StudentId FROM Student WHERE condition = xyz

我得到的输出像

<前>学生卡123676568990…………

存储过程的输出参数是@studentId字符串,我希望返回语句为

1236、7656、8990.

如何将输出转换为单个字符串?

我返回单列 [即.学生 ID]

解决方案

测试这个:

 DECLARE @result NVARCHAR(MAX)选择@result = 东西(( SELECT ',' + CONVERT(NVARCHAR(20), StudentId)来自学生WHERE 条件 = abcFOR xml路径('')), 1, 1, '')

I am getting the result in SQL Server as

SELECT StudentId FROM Student WHERE condition = xyz

I am getting the output like

StudentId
1236

7656

8990
........

The output parameter of the stored procedure is @studentId string and I want the return statement as

1236, 7656, 8990.

How can I convert the output in the single string?

I am returning single column [ie. StudentId]

解决方案

Test this:

 DECLARE @result NVARCHAR(MAX)

 SELECT @result = STUFF(
                        (   SELECT ',' + CONVERT(NVARCHAR(20), StudentId) 
                            FROM Student 
                            WHERE condition = abc 
                            FOR xml path('')
                        )
                        , 1
                        , 1
                        , '')

这篇关于将 SQL Server 结果集转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆