日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

SQL SERVER 2005允许自定义聚合函数

發(fā)布時間:2023/12/31 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL SERVER 2005允许自定义聚合函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

不多說了,說明后面是完整的代碼,用來將字符串型的字段的各行的值拼成一個大字符串,也就是通常所說的Concat

例如有如下表dict

?ID?NAME?CATEGORY
?1RED?COLOR?
?2BLUECOLOR
?3APPLE?FRUIT
?4ORANGEFRUIT

執(zhí)行SQL語句:select category,dbo.concatenate(name) as names from dict group by category.

得到結(jié)果表如下

?category?names
?COLORREDBLUE?
?FRUIT?APPLEORANGE

如果覺得需要用逗號或分號或其他任何你想要的分隔符分開,可以修改下面的代碼來實現(xiàn)。

?

在VS2005中,創(chuàng)建一個連接到目標庫的SQL SERVER PROJECT,然后填加一個“聚合”,將下面的代碼復制進去,編譯后,部署即可,然后在SQL SERVER中的“可編程性”“函數(shù)”“聚合函數(shù)”中就可以看到該函數(shù)了。?

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,這個方法是聚合最后調(diào)用的方法,它返回最后的值。可以是SQL?Server的任何標量;Accumulate,聚合每處理一行數(shù)據(jù)的時候都會調(diào)用一次,并將要處理的數(shù)據(jù)傳給方法。可以在函數(shù)內(nèi)部進行比如比較,合并之類的處理。


本文轉(zhuǎn)自 netcorner 博客園博客,原文鏈接:http://www.cnblogs.com/netcorner/archive/2013/04/13/3017693.html?? ,如需轉(zhuǎn)載請自行聯(lián)系原作者


總結(jié)

以上是生活随笔為你收集整理的SQL SERVER 2005允许自定义聚合函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。