发布:2022/12/22 16:45:53作者:管理员 来源:本站 浏览次数:574
IP地址转bigint类型,调用实例:select dbo.fn_get_ip_count('1.13.140.49')
运行结果:
USE [IP]
GO
/****** Object: UserDefinedFunction [dbo].[fn_get_ip_count] Script Date: 2022/12/22 16:41:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[fn_get_ip_count]
(
-- Add the parameters for the function here
@ip varchar(50)
)
RETURNS bigint
AS
BEGIN
-- Declare the return variable here
DECLARE @Word varchar(50)
DECLARE @count bigint
DECLARE @i int
set @count=0
set @i=0
DECLARE cursor_name CURSOR FOR --定义游标
SELECT Word FROM dbo.ufn_SplitToTable(@ip,'.') -- and IUIdInt in(40579)
OPEN cursor_name --打开游标
FETCH NEXT FROM cursor_name INTO @Word --抓取下一行游标数据
WHILE @@FETCH_STATUS = 0
BEGIN
--print(@PhoneStr)
if len(@Word)>=0
begin
--256 * 256 * 256 * long.Parse(arr[0]) + 256 * 256 * long.Parse(arr[1]) + 256 * long.Parse(arr[2]) + long.Parse(arr[3]);
if @i=0
begin
set @count=@count+256 * 256 * 256 * cast(@Word as decimal(18))
end
else if @i=1
begin
set @count=@count+256 * 256 * cast(@Word as decimal(18))
end
else if @i=2
begin
set @count=@count+256 * cast(@Word as decimal(18))
end
else
begin
set @count=@count+cast(@Word as decimal(18))
end
set @i= @i+1
end
FETCH NEXT FROM cursor_name INTO @Word
END
CLOSE cursor_name --关闭游标
DEALLOCATE cursor_name --释放游标
-- Return the result of the function
RETURN @count
END
GO
USE [IP]
GO调用实例:SELECT Word FROM dbo.ufn_SplitToTable('1.13.140.49','.')
运行结果:
1
13
140
49
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4