Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table '#source'. Scan count 1, logical reads 77, physical reads 0, read-ahead reads 0, lob logical reads 3803, lob physical reads 0, lob read-ahead reads 2381.SQL Server Execution Times: CPU time = 1422 ms, elapsed time = 1940 ms.
public partial class UserDefinedFunctions{ [Microsoft.SqlServer.Server.SqlFunction( FillRowMethodName = "FillRow", DataAccess = DataAccessKind.None, SystemDataAccess = SystemDataAccessKind.None, Name = "tfn_Split", TableDefinition = "row_id int, result nvarchar(max)")] public static IEnumerable tfn_Split ( [SqlFacet(IsNullable = false, MaxSize = -1, IsFixedLength = false)] SqlString StringToSplit, [SqlFacet(IsNullable = false, MaxSize = 4000, IsFixedLength = false)] SqlString Delimiter ) { string toSplit = StringToSplit.Value; string delimeter = Delimiter.Value; string[] items = toSplit.Split(new string[] { delimeter }, StringSplitOptions.RemoveEmptyEntries); Dictionary (items.Length); for (int i = 0; i < items.Length; i++) { results.Add(i + 1, items[i]); } return results; } public static void FillRow(Object obj, out SqlInt32 row_id, out SqlString result) { KeyValuePair )obj; row_id = new SqlInt32(kvp.Key); result = new SqlString(kvp.Value); }};
SELECT S.row_id, TVF.row_id, TVF.resultFROM #source AS SCROSSAPPLY dbo.tfn_Split(S.definition, CHAR(13) + CHAR(10)) AS TVFORDER BY S.row_id, TVF.row_id;
DELETE FROM @result -- Clean up-- //////////////////////////////////////////////////////////-- -> Clr xml solutionPRINT 'Start clr xml solution'SELECT @now = GETDATE()-- Split text into linesINSERT INTO @result SELECT l.line FROM @source s CROSS APPLY (SELECT ISNULL(T.C.value('(./text())[1]', 'nvarchar(1000)'), '') line FROM (SELECT dbo.ufn_clr_SplitLines(s.definition) col1) x CROSS APPLY x.col1.nodes('/Root/Item') T(C) ) l-- ResultsSELECT @duration = DATEDIFF(MILLISECOND, @now, GETDATE())SELECT @count = COUNT(*) FROM @resultPRINT 'Milliseconds: ' + CONVERT(VARCHAR(10), @duration) + ' | Lines: ' + CONVERT(VARCHAR(10), @count)-- <- Clr xml solution-- //////////////////////////////////////////////////////////