使用 SQL 而不是从键盘删除和结束 [英] Use SQL instead of Delete and End from keyboard

查看:21
本文介绍了使用 SQL 而不是从键盘删除和结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面运行一个简单的语句,输出如下:

I am running a simple statment below which gives the output as follow:

选择 '''' + name + '''' + ',' 作为来自 dbo.employee 的 Emp_Names

Emp_Names 
'Jason', 
'Robert', 
'Celia', 
'Linda', 
'David', 
'James', 
'Alison', 
'Chris', 
'Mary',

SQL 中有没有一种方法可以将我想要的输出显示为:

Is there a way in SQL that can show my desired output as:

Emp_Names 
'Jason', 'Robert','Celia','Linda','David','James','Alison','Chris','Mary',

我可以同时按下 Delete 和 End 来到达那里,但只能用于少数记录,但不能用于一百条记录...

i can press a Delete and End together to get there but only for a handful records but not for a hundred records...

谢谢大家!

我使用的是 SQL Server 2005 +

i am using SQL Server 2005 +

推荐答案

是的,但这取决于您使用的是哪个数据库?

Yes but it depends on which database are you using?

在 SQL Server 2005 或更高版本中,如果您希望将所有名称放在一列中,则可以使用 stuff 函数.

In SQL Server 2005 or later, you can use the stuff function if you want all the names in one column.

SELECT STUFF(( SELECT DISTINCT TOP 100 %',' + 名称来自员工ORDER BY ',' +姓名FOR XML 路径('')), 1, 1, '')或

SELECT STUFF(( SELECT DISTINCT TOP 100 PERCENT ',' + Name FROM employee ORDER BY ',' +Name FOR XML PATH('') ), 1, 1, '') or

选择 STUFF(( SELECT DISTINCT TOP 100 %''',''' + 名称来自员工ORDER BY ''',''' + 名称FOR XML 路径('')), 1, 2, '') + ''''
否则,您可以使用 pivot 命令将每个名称作为其自己的列.使用pivot 命令的唯一问题是您需要事先知道名称,否则您将与stuff 函数结合使用.

select STUFF(( SELECT DISTINCT TOP 100 PERCENT ''',''' + Name FROM employee ORDER BY ''',''' + Name FOR XML PATH('') ), 1, 2, '') + ''''
Otherwise you could use the pivot command to have each name as its own column. The only thing with using the pivot command is that you need to know the names before hand or else you would use it in conjunction with the stuff function.

这篇关于使用 SQL 而不是从键盘删除和结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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