Sql server 2008 逗号分隔的结果集+;SQL查询
我得到了两个数据表,如下所示: 表1:学生 表2:主题 我需要输出为: 我通过以下使用for XML PATH的查询实现了这一点 代码:Sql server 2008 逗号分隔的结果集+;SQL查询,sql-server-2008,tsql,sql-server-2008-r2,for-xml-path,Sql Server 2008,Tsql,Sql Server 2008 R2,For Xml Path,我得到了两个数据表,如下所示: 表1:学生 表2:主题 我需要输出为: 我通过以下使用for XML PATH的查询实现了这一点 代码: 我的问题是,有没有更好的方法不用使用XML路径就能做到这一点?这是一种非常好的方法,并且已经被广泛接受。有几种方法,这描述了很多方法 现有的一种有趣的方法是使用CLR为您完成工作,这将在运行外部代码的同时显著降低查询的复杂性。下面是该类在程序集中的外观示例 using System; using System.Collections.Generic; u
我的问题是,有没有更好的方法不用使用XML路径就能做到这一点?这是一种非常好的方法,并且已经被广泛接受。有几种方法,这描述了很多方法 现有的一种有趣的方法是使用CLR为您完成工作,这将在运行外部代码的同时显著降低查询的复杂性。下面是该类在程序集中的外观示例
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize=8000)]
public struct strconcat : IBinarySerialize{
private List values;
public void Init() {
this.values = new List();
}
public void Accumulate(SqlString value) {
this.values.Add(value.Value);
}
public void Merge(strconcat value) {
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate() {
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r) {
int itemCount = r.ReadInt32();
this.values = new List(itemCount);
for (int i = 0; i <= itemCount - 1; i++) {
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w) {
w.Write(this.values.Count);
foreach (string s in this.values) {
w.Write(s);
}
}
}
这显然要简单一点。不管它值多少钱:)
你好
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize=8000)]
public struct strconcat : IBinarySerialize{
private List values;
public void Init() {
this.values = new List();
}
public void Accumulate(SqlString value) {
this.values.Add(value.Value);
}
public void Merge(strconcat value) {
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate() {
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r) {
int itemCount = r.ReadInt32();
this.values = new List(itemCount);
for (int i = 0; i <= itemCount - 1; i++) {
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w) {
w.Write(this.values.Count);
foreach (string s in this.values) {
w.Write(s);
}
}
}
SELECT CategoryId,
dbo.strconcat(ProductName)
FROM Products
GROUP BY CategoryId ;