连接行 (sql server 2000)
我在使用 SQL Server 2000 时遇到以下问题
下表大约有 200 行。
Company / Employee 1005 / A 1005 / B 1005 / C 1010 / X 1010 / Y 1020 / L 1020 / M etc etc
我希望创建以下(逗号分隔)输出:
Company / Employees 1005 / A, B, C 1010 / X, Y 1020 / L, M etc etc
我在 SQL Server 2000 中遇到了这个问题,而 2005 似乎提供了更简单的解决方案来解决这个问题!我希望有人有一个好主意来解释如何解决这个问题......
I have the following problem when using SQL Server 2000
The following table has around 200 rows.
Company / Employee 1005 / A 1005 / B 1005 / C 1010 / X 1010 / Y 1020 / L 1020 / M etc etc
I wish to create the following (comma separated) output:
Company / Employees 1005 / A, B, C 1010 / X, Y 1020 / L, M etc etc
I'm having a really hard time with this in SQL Server 2000, while 2005 seems to offer easier solutions to solve this issue! I hope someone has a bright idea to explain how to solve this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我见过在 SQL 2000 中使用 UDF 的解决方案,但它们很可怕 - 它们基本上是为每个可能执行数百或数千次的组执行全新的
SELECT
。实际上,使用游标会更好:有一个使用临时表和 UPDATE 的解决方案,类似于用于计算运行总计的解决方案,但它看起来很麻烦,并且在大多数情况下不会这样做性能比光标好得多。
如果有人真的非常想看到
UPDATE
版本,我会发布它,但我建议首先尝试光标版本,看看它是否“足够好”。I've seen solutions using UDFs in SQL 2000, but they are scary - they're basically doing a brand-new
SELECT
for every group that might be executed hundreds or thousands of times. You're actually better off using a cursor:There's a solution using a temporary table and
UPDATE
similar to that used to compute running totals, but it's a beast to look at and in most cases won't do much better than the cursor for performance.If anyone really desperately wants to see the
UPDATE
version, I'll post it, but I suggest trying the cursor version first and seeing if it's "good enough."