本文将对MSDN中ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEV.v10.en/dnreal/html/realworld03112005.htm的用法进行改写,因为它针对的是Sql Server 2005 beta版本。


The following managed database objects are supported by SQL Server 2005 Beta 2:

  • Scalar-valued user-defined functions (scalar UDFs) 
  • User-defined aggregates (UDAs)
  • Table-valued user-defined functions (TVFs)
  • User-defined procedures (UDPs)
  • User-defined data types (UDTs)
  • User-defined triggers


下面将通过一个具体实际的例子展示用法:

1> 生成测试数据

Managed Database Objects: The CLR Enters the Relational Stagecreate table Person
Managed Database Objects: The CLR Enters the Relational Stage(
Managed Database Objects: The CLR Enters the Relational Stage   ID 
bigint NOT NULL identity (11primary key clustered,
Managed Database Objects: The CLR Enters the Relational Stage   FirstName 
nvarchar(50NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   LastName 
nvarchar(50NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   Country 
nvarchar(50NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   City 
nvarchar(50NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   ZipCode 
nvarchar(50NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   Street 
nvarchar(50NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   BirthDate 
smalldatetime NOT NULL,
Managed Database Objects: The CLR Enters the Relational Stage   Category 
int NOT NULL DEFAULT(0)
Managed Database Objects: The CLR Enters the Relational Stage)  
ON [PRIMARY]
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
declare @CategoryCount as int
Managed Database Objects: The CLR Enters the Relational Stage
declare @PersonCount as bigint
Managed Database Objects: The CLR Enters the Relational Stage
declare @PersonCountMax as bigint
Managed Database Objects: The CLR Enters the Relational Stage
declare @CountryCount as int
Managed Database Objects: The CLR Enters the Relational Stage
declare @RowCount as int
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
delete Person
Managed Database Objects: The CLR Enters the Relational Stage
set @RowCount = 0
Managed Database Objects: The CLR Enters the Relational Stage
set @CategoryCount = 0
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
--create categories
Managed Database Objects: The CLR Enters the Relational Stage
while @CategoryCount < 10
Managed Database Objects: The CLR Enters the Relational Stage
begin
Managed Database Objects: The CLR Enters the Relational Stage   
set @CategoryCount = @CategoryCount + 1
Managed Database Objects: The CLR Enters the Relational Stage   
set @CountryCount = 0
Managed Database Objects: The CLR Enters the Relational Stage   
--create countries in each category
Managed Database Objects: The CLR Enters the Relational Stage
   while @CountryCount < 10
Managed Database Objects: The CLR Enters the Relational Stage   
begin   
Managed Database Objects: The CLR Enters the Relational Stage      
set @CountryCount = @CountryCount + 1
Managed Database Objects: The CLR Enters the Relational Stage      
set @PersonCount = 0      
Managed Database Objects: The CLR Enters the Relational Stage      
--create a random number of persons
Managed Database Objects: The CLR Enters the Relational Stage
      set @PersonCountMax = rand() * 50      
Managed Database Objects: The CLR Enters the Relational Stage      
while @PersonCount < @PersonCountMax
Managed Database Objects: The CLR Enters the Relational Stage      
begin
Managed Database Objects: The CLR Enters the Relational Stage         
set @PersonCount = @PersonCount + 1      
Managed Database Objects: The CLR Enters the Relational Stage         
insert into 
Managed Database Objects: The CLR Enters the Relational Stage            Person       
Managed Database Objects: The CLR Enters the Relational Stage         
values
Managed Database Objects: The CLR Enters the Relational Stage         (
Managed Database Objects: The CLR Enters the Relational Stage            
'fn' + cast(@PersonCount as nvarchar(10)),
Managed Database Objects: The CLR Enters the Relational Stage            
'ln' + cast(@PersonCount as nvarchar(10)),
Managed Database Objects: The CLR Enters the Relational Stage            
'country' + cast(@CountryCount as nvarchar(10)),
Managed Database Objects: The CLR Enters the Relational Stage            
'SqlCity',
Managed Database Objects: The CLR Enters the Relational Stage            
'000000',
Managed Database Objects: The CLR Enters the Relational Stage            
'SqlStreet',
Managed Database Objects: The CLR Enters the Relational Stage            
GetDate(),
Managed Database Objects: The CLR Enters the Relational Stage            
@CategoryCount
Managed Database Objects: The CLR Enters the Relational Stage         )   
Managed Database Objects: The CLR Enters the Relational Stage         
set @RowCount = @RowCount + 1
Managed Database Objects: The CLR Enters the Relational Stage         
print @RowCount         
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage      
end
Managed Database Objects: The CLR Enters the Relational Stage   
end
Managed Database Objects: The CLR Enters the Relational Stage
end
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage


Requirements:


What we have here is a very simple table storing information about people. The business requirements specify that each person has to part of a single category, identified by an integer value. One day, our boss (the suite, for those who attended Tech-Ed 2004 in Amsterdam) comes to us with the following requirement—he wants to know, for each category of persons, which are the three countries having the largest number of persons in that particular category.



2> Aggregate

Managed Database Objects: The CLR Enters the Relational Stageusing System;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Text;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Data;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Data.SqlClient;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Data.SqlTypes;
Managed Database Objects: The CLR Enters the Relational Stage
using Microsoft.SqlServer.Server;
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage[Serializable]
Managed Database Objects: The CLR Enters the Relational Stage[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.UserDefined, 
Managed Database Objects: The CLR Enters the Relational Stage    IsInvariantToDuplicates
=false,
Managed Database Objects: The CLR Enters the Relational Stage    IsInvariantToNulls
=true,
Managed Database Objects: The CLR Enters the Relational Stage    IsInvariantToOrder
=false,
Managed Database Objects: The CLR Enters the Relational Stage    IsNullIfEmpty
=true,
Managed Database Objects: The CLR Enters the Relational Stage    MaxByteSize
=8000
Managed Database Objects: The CLR Enters the Relational Stage)]
Managed Database Objects: The CLR Enters the Relational Stage
public struct Aggregate_StrJoin:IBinarySerialize


使用方法:

select category,dbo.Aggregate_StrJoin(country) as countries from person
 group by category
 order by category


3> TVF

Managed Database Objects: The CLR Enters the Relational Stageusing System;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Data;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Collections;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Data.SqlClient;
Managed Database Objects: The CLR Enters the Relational Stage
using System.Data.SqlTypes;
Managed Database Objects: The CLR Enters the Relational Stage
using Microsoft.SqlServer.Server;
Managed Database Objects: The CLR Enters the Relational Stage
Managed Database Objects: The CLR Enters the Relational Stage
public partial class UserDefinedFunctions




感受到,使用C#写存储过程,用户函数的好处:

1. 可方面的使用.Net类库,进行文件操作等(sql不易处理)
2. 进行逻辑复杂的查询、统计等,使用CLS可提高效率
3. 可创建自己的聚合函数

相关文章: