SQL SERVER 2005允许自定义聚合函数
不多說了,說明后面是完整的代碼,用來將字符串型的字段的各行的值拼成一個大字符串,也就是通常所說的Concat
例如有如下表dict
| ?ID | ?NAME | ?CATEGORY |
| ?1 | RED? | COLOR? |
| ?2 | BLUE | COLOR |
| ?3 | APPLE? | FRUIT |
| ?4 | ORANGE | FRUIT |
執行SQL語句:select category,dbo.concatenate(name) as names from dict group by category.
得到結果表如下
| ?category | ?names |
| ?COLOR | REDBLUE? |
| ?FRUIT | ?APPLEORANGE |
如果覺得需要用逗號或分號或其他任何你想要的分隔符分開,可以修改下面的代碼來實現。
?
在VS2005中,創建一個連接到目標庫的SQL SERVER PROJECT,然后填加一個“聚合”,將下面的代碼復制進去,編譯后,部署即可,然后在SQL SERVER中的“可編程性”“函數”“聚合函數”中就可以看到該函數了。?
using System;
using System.Data;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
[Serializable]
[SqlUserDefinedAggregate(
??? Format.UserDefined, //use clr serialization to serialize the intermediate result
??? IsInvariantToNulls = true, //optimizer property
??? IsInvariantToDuplicates = false, //optimizer property
??? IsInvariantToOrder = false, //optimizer property
??? MaxByteSize = 8000) //maximum size in bytes of persisted value
]
public class Concatenate : IBinarySerialize
{
??? /// <summary>
??? /// The variable that holds the intermediate result of the concatenation
??? /// </summary>
??? private StringBuilder intermediateResult;
??? /// <summary>
??? /// Initialize the internal data structures
??? /// </summary>
??? public void Init()
??? {
??????? this.intermediateResult = new StringBuilder();
??? }
??? /// <summary>
??? /// Accumulate the next value, not if the value is null
??? /// </summary>
??? /// <param name="value"></param>
??? public void Accumulate(SqlString value)
??? {
??????? if (value.IsNull)
??????? {
??????????? return;
??????? }
??????? this.intermediateResult.Append(value.Value);
??? }
??? /// <summary>
??? /// Merge the partially computed aggregate with this aggregate.
??? /// </summary>
??? /// <param name="other"></param>
??? public void Merge(Concatenate other)
??? {
??????? this.intermediateResult.Append(other.intermediateResult);
??? }
??? /// <summary>
??? /// Called at the end of aggregation, to return the results of the aggregation.
??? /// </summary>
??? /// <returns></returns>
??? public SqlString Terminate()
??? {
??????? string output = string.Empty;
??????? //delete the trailing comma, if any
??????? if (this.intermediateResult != null
??????????? && this.intermediateResult.Length > 0)
??????? {
??????????? output = this.intermediateResult.ToString(0, this.intermediateResult.Length );
??????? }
??????? return new SqlString(output);
??? }
??? public void Read(BinaryReader r)
??? {
??????? intermediateResult = new StringBuilder(r.ReadString());
??? }
??? public void Write(BinaryWriter w)
??? {
??????? w.Write(this.intermediateResult.ToString());
??? }
}
?
這里有幾個比較重要的方法:Terminate,這個方法是聚合最后調用的方法,它返回最后的值。可以是SQL?Server的任何標量;Accumulate,聚合每處理一行數據的時候都會調用一次,并將要處理的數據傳給方法。可以在函數內部進行比如比較,合并之類的處理。
本文轉自 netcorner 博客園博客,原文鏈接:http://www.cnblogs.com/netcorner/archive/2013/04/13/3017693.html?? ,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的SQL SERVER 2005允许自定义聚合函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 京东如何开店
- 下一篇: 如何删除一个VDP服务器