We create a new database project, and then we build a userdefinedfunctions,code like this:
Copy Code code as follows:
1:///<summary>
Sqls the array.
</summary>
<param name= "str" >the str.</param>
<param name= "delimiter" >the delimiter.</param>
<returns></returns>
1/8/2010 2:41 PM Author:v-pliu
[SqlFunction (Name = "Clr_split",
FillRowMethodName = "FillRow",
tabledefinition = "id nvarchar (10)")]
public static IEnumerable Sqlarray (SqlString str, SqlChars delimiter)
{
if (delimiter. Length = = 0)
return new string[1] {str. Value};
Return str. Value.split (Delimiter[0]);
}
<summary>
Fills the row.
</summary>
<param name= "Row" >the row.</param>
<param name= "str" >the str.</param>
1/8/2010 2:41 PM Author:v-pliu
public static void FillRow (object row, out SqlString str)
{
str = new SqlString ((string) row);
}
Then Bulid,deploy all OK, execute the following test T-SQL in SSMS:
Copy Code code as follows:
DECLARE @array VARCHAR (max)
SET @array = ' 39,15,93,68,64,43,90,58,39,9,26,26,89,47,91,57,98,16,55,9,63,29,69,16,41,76,34,60,68,64,61,53,32,30,11,72,57,63,36,43,22 , 14,60,38,24,5,66,26,26,21,22,99,55,18,7,10,46,76,27,88,9,29,89,75,48,72,94,59,35,19,0,35,79,11,87,49,68,30,91,35,9,7,34 , 47,41,61,98,13,22,1,26,80,35,48,34,92,24,85,90,51 ' SELECT ID from dbo. Clr_split (@array, ', ')
Let's look at its client statistic:
Then we execute the test T-SQL using the same array:
Copy Code code as follows:
DECLARE @array VARCHAR (max)
SET @array = ' 39,15,93,68,64,43,90,58,39,9,26,26,89,47,91,57,98,16,55,9,63,29,69,16,41,76,34,60,68,64,61,53,32,30,11,72,57,63,36,43,22 , 14,60,38,24,5,66,26,26,21,22,99,55,18,7,10,46,76,27,88,9,29,89,75,48,72,94,59,35,19,0,35,79,11,87,49,68,30,91,35,9,7,34 , 47,41,61,98,13,22,1,26,80,35,48,34,92,24,85,90,51 '
SELECT item from Strtotable (@array, ', ')
The client statistic of the split function implemented by the CTE:
By contrast, you can see that the CLR's performance is slightly higher than the CTE method, because the CLR has cache capabilities and it is more efficient to put a complex operation into the program than in the database.
You can also refer to:
Split string in SQL Server 2005+ CLR vs. T-SQL
Author:petter Liu