亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

如何:創(chuàng)建和運(yùn)行 CLR SQL Server 聚合

瀏覽:141日期:2023-11-07 10:16:15

通過(guò)向 SQL Server 項(xiàng)目添加“聚合”項(xiàng)創(chuàng)建 SQL 聚合。部署成功后,在托管代碼中創(chuàng)建的聚合像其他任何 SQL Server 聚合一樣被調(diào)用和執(zhí)行。

注意; 在默認(rèn)情況下,Microsoft SQL Server 中關(guān)閉了公共語(yǔ)言運(yùn)行庫(kù) (CLR) 集成功能。必須啟用該功能才能使用 SQL Server 項(xiàng)目項(xiàng)。若要啟用 CLR 集成,請(qǐng)使用 sp_configure 存儲(chǔ)過(guò)程的“啟用 clr”選項(xiàng)。有關(guān)更多信息,請(qǐng)參見(jiàn) 啟用 CLR 集成。注意; SQL Server 集合要求實(shí)現(xiàn)四個(gè)特別方法:Init、Accumulate、Merge 和 Terminate。有關(guān)更多信息,請(qǐng)參見(jiàn)《SQL Books Online》(SQL 聯(lián)機(jī)叢書(shū))中的“SQL CLR .NET User-Defined Aggregate Functions”(SQL CLR .NET 用戶定義的聚合函數(shù))主題。注意; 顯示的對(duì)話框和菜單命令可能會(huì)與幫助中的描述不同,具體取決于您現(xiàn)用的設(shè)置或版本。若要更改設(shè)置,請(qǐng)?jiān)凇肮ぞ摺辈藛紊线x擇“導(dǎo)入和導(dǎo)出設(shè)置”。有關(guān)更多信息,請(qǐng)參見(jiàn) Visual Studio 設(shè)置。

創(chuàng)建 SQL Server 聚合創(chuàng)建 SQL Server 聚合打開(kāi)一個(gè)現(xiàn)有的“SQL Server 項(xiàng)目”,或者創(chuàng)建一個(gè)新項(xiàng)目。有關(guān)更多信息,請(qǐng)參見(jiàn) 如何:創(chuàng)建 SQL Server 項(xiàng)目。

從“項(xiàng)目”菜單中選擇“添加新項(xiàng)”。

在 “添加新項(xiàng)”對(duì)話框 中選擇“聚合”。

輸入新聚合的“名稱(chēng)”。

添加執(zhí)行聚合時(shí)要運(yùn)行的代碼。請(qǐng)參見(jiàn)下面的第一個(gè)示例。

注意; C++ 示例在編譯時(shí)必須使用 /clr:safe 編譯器選項(xiàng)。

將聚合部署到 SQL Server。有關(guān)更多信息,請(qǐng)參見(jiàn) 如何:將 SQL Server 項(xiàng)目項(xiàng)部署到 SQL Server 中。

通過(guò)在 SQL Server 上執(zhí)行聚合對(duì)其進(jìn)行調(diào)試。請(qǐng)參見(jiàn)下面的第二個(gè)示例。

示例此示例創(chuàng)建對(duì)元音計(jì)數(shù)的聚合。此聚合對(duì)字符串?dāng)?shù)據(jù)類(lèi)型的列中的元音計(jì)數(shù)。聚合包含以下四個(gè)必需的可運(yùn)行多個(gè)線程的方法:Init、Accumulate、Merge 和 Terminate。

Visual Basic 復(fù)制代碼Imports SystemImports System.Data.SqlTypesImports Microsoft.SqlServer.Server

<Serializable()> _<SqlUserDefinedAggregate(Format.Native)> _Public Structure CountVowels

' count only the vowels in the passed-in strings Private countOfVowels As SqlInt32

Public Sub Init() countOfVowels = 0 End Sub

Public Sub Accumulate(ByVal value As SqlString) Dim stringChar As String Dim indexChar As Int32

' for each character in the given parameter For indexChar = 0 To Len(value.ToString()) - 1

stringChar = value.ToString().Substring(indexChar, 1)

If stringChar.ToLower() Like '[aeiou]' Then

' it is a vowel, increment the count countOfVowels = countOfVowels + 1 End If Next End Sub

Public Sub Merge(ByVal value As CountVowels)

Accumulate(value.Terminate()) End Sub

Public Function Terminate() As SqlString

Return countOfVowels.ToString() End FunctionEnd StructureC# 復(fù)制代碼using System;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;

[Serializable][SqlUserDefinedAggregate(Format.Native)]public struct CountVowels{ // count only the vowels in the passed-in strings private SqlInt32 countOfVowels;

public void Init() { countOfVowels = 0; }

public void Accumulate(SqlString value) { // list of vowels to look for string vowels = 'aeiou'; // for each character in the given parameter for (int i=0; i < value.ToString().Length; i++) { // for each character in the vowels string for (int j=0; j < vowels.Length; j++) { // convert parameter character to lowercase and compare to vowel if (value.Value.Substring(i,1).ToLower() == vowels.Substring(j,1)) { // it is a vowel, increment the count countOfVowels+=1; } } } }

public void Merge(CountVowels value) { Accumulate(value.Terminate()); }

public SqlString Terminate() { return countOfVowels.ToString(); }}C++ 復(fù)制代碼#include 'stdafx.h'

#using <System.dll>#using <System.Data.dll>#using <System.Xml.dll>

using namespace System;using namespace System::Data;using namespace System::Data::Sql;using namespace System::Data::SqlTypes;using namespace Microsoft::SqlServer::Server;

// In order to debug your Aggregate, add the following to your debug.sql file://// SELECT LastName, COUNT(LastName) AS CountOfLastName, dbo.CountVowels(LastName) AS CountOfVowels// FROM Person.Contact// GROUP BY LastName// ORDER BY LastName//

[Serializable][SqlUserDefinedAggregate(Format::Native)]public value struct CountVowels{public: void Init() { countOfVowels = 0; }

void Accumulate(SqlString value) { // list of vowels to look for String ^vowels = 'aeiou';

// for each character in the given parameter for (int i=0; i < value.ToString()->Length; i++) { // for each character in the vowels string for (int j=0; j < vowels->Length; j++) { // convert parameter character to lowercase and compare to vowel if (value.Value->Substring(i, 1)->ToLower() == vowels->Substring(j, 1)) { // it is a vowel, increment the count countOfVowels+=1; break; } } } }

void Merge(CountVowels value) { Accumulate(value.Terminate()); }

SqlTypes::SqlString Terminate() { return countOfVowels.ToString(); }

private: // count only the vowels in the passed-in strings SqlInt32 countOfVowels;};

部署聚合后,在 SQL Server 上運(yùn)行它以進(jìn)行調(diào)試并驗(yàn)證是否返回正確的數(shù)據(jù)。此查詢返回對(duì) Contact 表中 LastNames 列的所有值的元音計(jì)數(shù)的結(jié)果集。

復(fù)制代碼SELECT LastName, COUNT(LastName) AS CountOfLastName, dbo.CountVowels(LastName) AS CountOfVowelsFROM Person.ContactGROUP BY LastNameORDER BY LastName

主站蜘蛛池模板: 麻豆视频免费观看入口 | 黄色一级片观看 | 国产乱视频在线观看播放 | xxoo做爰猛烈动态视频网站 | 俺来也久久 | 欧美成人全部免费观看1314色 | 97视频免费播放观看在线视频 | 欧美日韩亚洲一区二区三区在线观看 | 欧美中文字幕一二三四区 | 欧美日本日韩 | 国产一区二区视频在线播放 | 夜色55夜色66亚洲精品网站 | 亚洲一级毛片免费观看 | 99久久国产综合精品网成人影院 | 国产呦精品一区二区三区网站 | 亚洲精品第一国产综合高清 | 黄网站视频观看免费 | 欧洲美女粗暴交视频 | 免费欧美黄色片 | 黄色网址发给我 | 亚洲国产成人精品一区二区三区 | 亚洲综合91社区精品福利 | 在线欧美日韩国产 | 福利一区二区三区视频在线观看 | 香港a毛片 | 国产福利在线观看第二区 | 精品日韩二区三区精品视频 | 黄色片免费播放 | 国产精品视频九九九 | 欧美一级成人一区二区三区 | 在线看91| 美女一级毛片免费不卡视频 | 天天成人综合网 | 日韩免费在线视频观看 | 日韩成人性视频 | 中国国产一国产一级毛片视频 | 麻豆国产高清在线播放 | 蕾丝视频www在线观看 | 黄色一级一毛片 | 亚洲精品国产成人7777 | 亚洲人在线 |