日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

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

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

例如有如下表dict

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

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

得到結果表如下

?category?names
?COLORREDBLUE?
?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,聚合每處理一行數據的時候都會調用一次,并將要處理的數據傳給方法??梢栽诤瘮祪炔窟M行比如比較,合并之類的處理。


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


總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。