av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

瀏覽:163日期: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)參見 啟用 CLR 集成。注意; SQL Server 集合要求實(shí)現(xiàn)四個(gè)特別方法:Init、Accumulate、Merge 和 Terminate。有關(guān)更多信息,請(qǐng)參見《SQL Books Online》(SQL 聯(lián)機(jī)叢書)中的“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)參見 Visual Studio 設(shè)置。

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

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

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

輸入新聚合的“名稱”。

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

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

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

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

示例此示例創(chuàng)建對(duì)元音計(jì)數(shù)的聚合。此聚合對(duì)字符串?dāng)?shù)據(jù)類型的列中的元音計(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

主站蜘蛛池模板: 波多野结衣一区二区三区在线观看 | 亚洲一区在线看 | 久久久九九| 久久精品99久久久久久 | 日本免费一级 | a视频在线免费观看 | 国产又粗又猛视频免费 | 久久精品在线观看 | 97精品在线视频 | 狠狠做深爱婷婷久久综合一区 | 国产不卡视频 | 久草资源在线观看 | 成人免费毛片嘿嘿连载视频 | 欧美日韩免费一区二区三区 | 欧美久久久久久久 | 免费视频a| 婷婷第四色 | 免费看毛片的网站 | 欧美做受喷浆在线观看 | 亚洲一区免费视频 | 亚洲精品在线视频观看 | 成人黄色在线观看 | 欧美一级片免费看 | 毛片免费在线观看 | 国内精品视频 | 黄色大片在线播放 | 免费观看一区二区 | 亚洲欧美中文字幕 | 91狠狠综合 | 美女黄色大片 | 亚洲第一色网 | 久久靖品| 精品视频一区二区三区 | 超碰com | 欧美成人精品欧美一级私黄 | 欧美性久久 | 欧美视频一区二区三区 | 日韩视频一区 | 国产精品黄色 | 香蕉看片 | 青草视频在线播放 |