Наконец, примерно через 10 лет после Oracle, sqlserver получил собственную компиляцию (с ограничениями)
ALTER function fn_ValidateIPv4
(
@ip varchar(255)
)
RETURNS int
--WITH EXECUTE AS OWNER, SCHEMABINDING, NATIVE_COMPILATION
AS
BEGIN
--ATOMIC WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
/* only sql2016 native Compilation **/
DECLARE @len_ip as int;
SET @len_ip = len(@ip);
DECLARE @firstBlock varchar(4) = '';
DECLARE @secondBlock varchar(4) = '';
DECLARE @thirdBlock varchar(4) = '';
DECLARE @fourthBlock varchar(4) = '';
DECLARE @countDot as smallint = 0;
DECLARE @l_i as smallint = 0;
DECLARE @l_curChar varchar(1) = 'X';
DECLARE @Result int = 0
IF (@len_ip <= 15)
BEGIN
WHILE (@l_i < @len_ip)
BEGIN
set @l_i += 1;
set @l_curChar = substring(@ip,@l_i,1);
if @l_curChar = '.'
SET @countDot += 1
ELSE
BEGIN
IF @l_curChar IN ( '0','1','2','3','4','5','6','7','8','9' )
BEGIN
IF @countDot = 0
SET @firstBlock = @firstBlock + @l_curChar;
IF @countDot = 1
SET @secondBlock = @secondBlock + @l_curChar;
IF @countDot = 2
SET @thirdBlock = @thirdBlock + @l_curChar;
IF @countDot = 3
SET @fourthBlock = @fourthBlock + @l_curChar;
IF @countDot > 3
set @firstBlock = 'AAA'; -- force error
END
ELSE set @firstBlock = 'AAA'; -- force error
END;
END;
IF ( @countDot = 3 and
cast(@fourthBlock as int) between 1 and 255 and
cast(@thirdBlock as int) between 0 and 255 and
cast(@secondBlock as int) between 0 and 255 and
cast(@firstBlock as int) between 0 and 255
)
set @Result = 1;
END;
/*
select dbo.fn_ValidateIPv4( '127.0.0.258' );
*/
RETURN @Result
END;
Мне пришлось удалить не поддерживаемые встроенные функции - числовые и т. Д. *