sp_configure 'clr enabled' ;
name minimum maximum config_value run_value
----------------------------------- ----------- ----------- ------------ -----------
clr enabled 0 1 0 0
|
sp_configure 'clr enabled' , 1;
GO
RECONFIGURE ;
GO
sp_configure 'clr enabled' ;
name minimum maximum config_value run_value
----------------------------------- ----------- ----------- ------------ -----------
clr enabled 0 1 1 1
|
using System;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_HelloSqlClr" )]
public static SqlString HelloSqlClr(SqlString input)
{
return input;
}
}
|
csc /target:library /out:SqlServer.SqlClr.Functions.dll UserDefinedFunctions.cs
|
CREATE ASSEMBLY [SqlServer.SqlClr.Functions]
FROM 'D:/SqlServer SqlClr Solution/SqlServer.SqlClr.Functions/SqlServer.SqlClr.Functions.dll'
WITH PERMISSION_SET = SAFE
|
SELECT * FROM sys . assemblies
|
CREATE FUNCTION [dbo] . [Clr_HelloSqlClr] ( @input [nvarchar] ( 128))
RETURNS [nvarchar] ( 128) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlServer.SqlClr.Functions] . [UserDefinedFunctions] . [HelloSqlClr]
|
SELECT dbo . Clr_HelloSqlClr ( 'Hello sqlclr' );
---------------------------
Hello sqlclr
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_Base64Encode" )]
public static SqlString Base64Encode(SqlString input)
{
if (input.IsNull)
{
return new SqlString (null );
}
byte [] array = System.Text.Encoding .UTF8.GetBytes(input.Value);
string result = System.Convert .ToBase64String(array);
return new SqlString (result);
}
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_Base64Decode" )]
public static SqlString Base64Decode(SqlString input)
{
if (input.IsNull)
{
return new SqlString (null );
}
byte [] array = System.Convert .FromBase64String(input.Value);
string result = Encoding .UTF8.GetString(array);
return new SqlString (result);
}
|
ALTER FUNCTION [dbo] . [Clr_Base64Encode] ( @input [nvarchar] ( 4000))
RETURNS [nvarchar] ( 4000) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlServer.SqlClr.Functions] . [UserDefinedFunctions] . [Base64Encode]
|
SELECT dbo . Clr_Base64Encode ( 'StarCraft|WarCraft|Diablo' ) AS ITEM ;
SELECT dbo . Clr_Base64Decode ( 'U3RhckNyYWZ0fFdhckNyYWZ0fERpYWJsbw==' ) AS ITEM ;
ITEM
-------------------------------------------------------
U3RhckNyYWZ0fFdhckNyYWZ0fERpYWJsbw==
ITEM
-------------------------------------------------------
StarCraft|WarCraft|Diablo
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_RegexLike" )]
public static SqlBoolean RegexLike(SqlString input, SqlString pattern, SqlInt32 options)
{
if (input.IsNull || pattern.IsNull)
{
return new SqlBoolean (false );
}
bool result = Regex .IsMatch(input.Value, pattern.Value, (RegexOptions )options.Value);
return new SqlBoolean (result);
}
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_RegexMatchIndex" )]
public static SqlInt32 RegexMatchIndex(SqlString input, SqlString pattern, SqlInt32 options)
{
if (input.IsNull || pattern.IsNull)
{
return new SqlInt32 (-1);
}
Match match = Regex .Match(input.Value, pattern.Value, (RegexOptions )options.Value);
if (match.Success)
{
return new SqlInt32 (match.Captures[0].Index);
}
return new SqlInt32 (-1);
}
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_RegexMatchValue" )]
public static SqlString RegexMatchValue(SqlString input, SqlString pattern, SqlInt32 options)
{
if (input.IsNull || pattern.IsNull)
{
return SqlString .Null;
}
Match match = Regex .Match(input.Value, pattern.Value, (RegexOptions )options.Value);
if (match.Success)
{
return new SqlString (match.Captures[0].Value);
}
return SqlString .Null;
}
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_RegexReplace" )]
public static SqlString RegexReplace(SqlString input, SqlString pattern, SqlString replacement, SqlInt32 options)
{
if (input.IsNull || pattern.IsNull || replacement.IsNull)
{
return input;
}
string s = Regex .Replace(input.Value, pattern.Value, replacement.Value, (RegexOptions )options.Value);
return new SqlString (s);
}
|
[Test ]
public void RegexOptionsTest()
{
Assert .AreEqual((int )RegexOptions .None,0);
Assert .AreEqual((int )RegexOptions .IgnoreCase,1);
Assert .AreEqual((int )RegexOptions .Multiline,2);
Assert .AreEqual((int )RegexOptions .ExplicitCapture,4);
Assert .AreEqual((int )RegexOptions .Compiled,8);
Assert .AreEqual((int )RegexOptions .Singleline,16);
Assert .AreEqual((int )RegexOptions .IgnorePatternWhitespace,32);
Assert .AreEqual((int )RegexOptions .RightToLeft,64);
Assert .AreEqual((int )RegexOptions .ECMAScript, 256);
Assert .AreEqual((int )RegexOptions .CultureInvariant,512);
Assert .AreEqual((int )(RegexOptions .IgnoreCase | RegexOptions .Multiline), 3);
}
|
CREATE TABLE [dbo] . [GameInfo] (
[Id] [int] NOT NULL,
[Name] [nvarchar] ( 32) NOT NULL
) ON [PRIMARY]
GO
INSERT INTO [dbo] . [GameInfo] ( Id , Name ) VALUES ( 1, 'StarCraft' );
INSERT INTO [dbo] . [GameInfo] ( Id , Name ) VALUES ( 2, 'WarCraft' );
INSERT INTO [dbo] . [GameInfo] ( Id , Name ) VALUES ( 1, 'Diablo' );
|
SELECT * FROM dbo . GameInfo WHERE dbo . Clr_RegexLike ( Name , 'Craft$' , 0) = 1;
Id Name
----------- --------------------------------
1 StarCraft
2 WarCraft
SELECT Name , dbo . Clr_RegexMatchIndex ( Name , 'Craft$' , 0) AS MIndex FROM dbo . GameInfo ;
Name MIndex
-------------------------------- -----------
StarCraft 4
WarCraft 3
Diablo -1
SELECT Name , dbo . Clr_RegexMatchValue ( Name , 'craft$' , 1) AS MValue FROM dbo . GameInfo ;
Name MValue
-------------------------------- ------------------------------
StarCraft Craft
WarCraft Craft
Diablo NULL
SELECT Name , dbo . Clr_RegexReplace ( Name , '^StarCraft$' , 'StarCraftII' , 0) AS Name2 FROM dbo . GameInfo ;
Name Name2
-------------------------------- -------------------------------
StarCraft StarCraftII
WarCraft WarCraft
Diablo Diablo
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_Split" , FillRowMethodName = "SplitFillRow" , TableDefinition = "item nvarchar(256)" )]
public static IEnumerable Split(SqlString input, SqlString separators)
{
string [] array;
if (input.IsNull)
{
array = new string [] { null };
}
else if (separators.IsNull)
{
array = new string [] { input.Value };
}
else
{
string [] separatorsArray = separators.Value.Split(new char [] { ',' }, StringSplitOptions .RemoveEmptyEntries);
array = input.Value.Split(separatorsArray, StringSplitOptions .None);
}
return array;
}
private static void SplitFillRow(Object obj, ref SqlString item)
{
if (obj != null )
{
item = (string )obj;
}
}
|
ALTER FUNCTION [dbo] . [Clr_Split] ( @input [nvarchar] ( 4000), @separators [nvarchar] ( 4000))
RETURNS TABLE (
[item] [nvarchar] ( 256) NULL
) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlServer.SqlClr.Functions] . [UserDefinedFunctions] . [Split]
|
SELECT * FROM dbo . Clr_Split ( 'StarCraft|WarCraft|Diablo' , '|' );
SELECT * FROM dbo . Clr_Split ( 'StarCraft|WarCraft//Diablo' , '|,//' );
item
-----------------------------
StarCraft
WarCraft
Diablo
|
[Microsoft.SqlServer.Server.SqlFunction (Name = "Clr_SplitWithOrder" , FillRowMethodName = "SplitWithOrderFillRow" , TableDefinition = "orderId int, item nvarchar(4000)" )]
public static IEnumerable SplitWithOrder(SqlString input, SqlString separators)
{
Dictionary <int , string > dictionary = new Dictionary <int , string >();
if (input.IsNull)
{
dictionary.Add(1, null );
}
else if (separators.IsNull)
{
dictionary.Add(1, input.Value);
}
else
{
string [] separatorsArray = separators.Value.Split(new char [] { ',' }, StringSplitOptions .RemoveEmptyEntries);
string [] array = input.Value.Split(separatorsArray, StringSplitOptions .None);
for (int i = 0; i < array.Length; i++)
{
dictionary.Add(i + 1, array[i]);
}
}
return dictionary;
}
private static void SplitWithOrderFillRow(Object obj, ref SqlInt32 orderid, ref SqlString item)
{
if (obj != null )
{
KeyValuePair <int , string > kvp = (KeyValuePair <int , string >)obj;
orderid = kvp.Key;
item = kvp.Value;
}
}
|
SELECT * FROM dbo . Clr_SplitWithOrder ( 'StarCraft|WarCraft//Diablo' , '|,//' ) ORDER BY orderId DESC ;
orderId item
----------- ----------------------------------------
3 Diablo
2 WarCraft
1 StarCraft
|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
[Serializable ]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate (Format .Native)]
public struct Aggregate1
{
public void Init()
{
}
public void Accumulate(SqlString Value)
{
}
public void Merge(Aggregate1 Group)
{
}
public SqlString Terminate()
{
return new SqlString ("" );
}
private int var1;
}
|
public void Init();
|
public void Accumulate(input_type Value);
|
public void Merge(Aggregate1 Group);
|
public return_type Terminate();
|
[Serializable ]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate (Format .Native,IsInvariantToNulls = true , IsInvariantToDuplicates = false )]
public struct QuadraticMean
{
private double totalValue;
private int count;
public void Init()
{
totalValue = 0.0;
count = 0;
}
public void Accumulate(SqlDouble input)
{
if (input.IsNull)
{
return ;
}
totalValue += input.Value * input.Value;
count++;
}
public void Merge(QuadraticMean Group)
{
totalValue += Group.totalValue;
count = Group.count;
}
public SqlDouble Terminate()
{
double result = Math .Sqrt(totalValue / count);
return new SqlDouble (result);
}
}
|
CREATE AGGREGATE [dbo] . [QuadraticMean]
( @input [float] )
RETURNS [float]
EXTERNAL NAME [SqlServer.SqlClr.Functions] . [QuadraticMean]
|
SELECT avg ( Id ), dbo . QuadraticMean ( Id ) FROM dbo . GameInfo ;
----------- ----------------------
2 2.16024689946929
|