ALTER Function [dbo].[split](
@InputText Varchar(max),
@Delimiter Varchar(10))
RETURNS @Array TABLE (
TokenID Int PRIMARY KEY IDENTITY(1,1),
Value Varchar(max))
AS
BEGIN
DECLARE
@Pos Int,
@End Int,
@TextLength Int,
@DelimLength Int
SET @TextLength = DataLength(@InputText)
IF @TextLength = 0 RETURN
SET @Pos = 0
SET @DelimLength = DataLength(@Delimiter)
IF @DelimLength = 0 BEGIN
WHILE @Pos <= @TextLength BEGIN
INSERT @Array (Value) VALUES (SubString(@InputText,@Pos,1))
SET @Pos = @Pos + 1
END
END
ELSE BEGIN
-- Tack on delimiter to 'see' the last token
SET @InputText = @InputText + @Delimiter
-- Find the end character of the first token
SET @End = CharIndex(@Delimiter, @InputText)
WHILE @End > 0 BEGIN
-- End > 0, a delimiter was found: there is a(nother) token
INSERT @Array (Value) VALUES (SubString(@InputText, @Pos, @End - @Pos))
-- Set next search to start after the previous token
SET @Pos = @End + @DelimLength
-- Find the end character of the next token
SET @End = CharIndex(@Delimiter, @InputText, @Pos)
END
END
RETURN
END
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[SqlFunction(FillRowMethodName = "SplitStringFillRow", TableDefinition = "part NVARCHAR(MAX), ID_ORDER INT")]
static public IEnumerator SplitString(string text, string delimiter)
{
int text_len = text.Length;
int delimeter_len = delimiter.Length;
int pos = 0;
int count = 0;
if (text_len == 0)
{
yield break;
}
if (delimeter_len == 0)
{
for(int i = 0; i < text_len; i++)
{
yield return new KeyValuePair<int, string>(i+1, text[i].ToString());
}
}
else
{
text = text + delimiter;
int end = text.IndexOf(delimiter);
if (text.Substring(pos, end - pos + delimeter_len) == delimiter)
{
count += 1;
yield return new KeyValuePair<int, string>(count, text.Substring(pos, end - pos));
pos = end + delimeter_len;
end = text.IndexOf(delimiter, pos);
}
while (end > 0)
{
count += 1;
yield return new KeyValuePair<int, string>(count, text.Substring(pos, end - pos));
pos = end + delimeter_len;
end = text.IndexOf(delimiter, pos);
}
}
}
static public void SplitStringFillRow(object oKeyValuePair, out SqlString value, out SqlInt32 valueIndex)
{
KeyValuePair<int, string> keyValuePair = (KeyValuePair<int, string>)oKeyValuePair;
valueIndex = keyValuePair.Key;
value = keyValuePair.Value;
}
}
DECLARE @Command VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::<databasename> TO
[<<loginname>>]'
SELECT @Command = REPLACE(REPLACE(@Command
, '<databasename>', SD.Name)
, '<loginname>', SL.Name)
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = DB_NAME()
EXEC(@Command) </loginname>
CREATE FUNCTION [dbo].SplitStringCLR(@text [nvarchar](max), @delimiter [nvarchar](max))
RETURNS TABLE (
part nvarchar(max),
ID_ODER int
) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME Database2.UserDefinedFunctions.SplitString*/
Количество строк
на вход
Время работы
CLR функции
Время работы
T-SQL функции
10000
100000
1000000