如何把这两个ASP格式化IP的函数写与SQL自定义函数

qfljm 2006-02-23 10:50:48
如何把这两个ASP格式化IP的函数写与SQL自定义函数

Function EcodeIP(strIP)
Dim aIP, RV
If strIP="" Or strIP="127.0.0.1" Then strIP="192.168.0.1"
aIP = Split(strIP, ".")
EcodeIP=CLng(aIP(0)) * 16777216 + CLng(aIP(1)) * 65536 + CLng(aIP(2)) * 256 + CLng(aIP(3)) - 1
End Function

Function DcodeIP(intIP)
Dim sect1, sect2, sect3, sect4
sect1 = Int(intIP / 16777216)
intIP = intIP - sect1 * 16777216
sect2 = Int(intIP / 65536)
intIP = intIP - sect2 * 65536
sect3 = Int(intIP / 256)
intIP = intIP - sect3 * 256
sect4 = intIP+1
DcodeIP = sect1 & "." & sect2 & "." & sect3 & "." & sect4
End Function

MSSQL2000版本
...全文
150 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2006-02-23
  • 打赏
  • 举报
回复
如果你的函数是返回值的, 则调用时必须加所有者, 也就是楼主说的dbo

如果你的函数是返回表的, 则调用时直接用函数名即可.
zlp321002 2006-02-23
  • 打赏
  • 举报
回复
----------
dbo 是具有在数据库中执行所有活动的暗示性权限的用户。
固定服务器角色 sysadmin 的任何成员创建的任何对象都自动属于 dbo。
qfljm 2006-02-23
  • 打赏
  • 举报
回复
所有这样的自定义函数都要加上
dbo
才能运行吗?
zlp321002 2006-02-23
  • 打赏
  • 举报
回复
Create function EcodeIP(@strIP as varchar(2000))
returns bigint
as
Begin
if @strIP='' or @strIP='127.0.0.1'
begin
set @strIP='192.168.0.1'
end
return(
cast(PARSENAME(@strIP,4) as bigint) * 16777216 +cast(PARSENAME(@strIP,3) as bigint)* 65536+
cast(PARSENAME(@strIP,2) as bigint) * 256+cast(PARSENAME(@strIP,1) as bigint)-1
)
End

Create function DcodeIP(@strIP as bigint)
returns varchar(2000)
as
Begin
declare @sect1 bigint,@sect2 bigint,@sect3 bigint,@sect4 bigint,@tempstrIP bigint
set @sect1=cast(@strIP/16777216 as bigint)
set @tempstrIP=@strIP-@sect1*16777216
set @sect2=cast(@tempstrIP/65536 as bigint)
set @tempstrIP=@tempstrIP-@sect2*65536
set @sect3=cast(@tempstrIP/256 as bigint)
set @tempstrIP=@tempstrIP-@sect3*256
set @sect4=@tempstrIP+1
return cast(@sect1 as varchar)+'.'+cast(@sect2 as varchar)+'.'+cast(@sect3 as varchar)+'.'+cast(@sect4 as varchar)
End

--测试:
select dbo.EcodeIP('211.23.44.1')
/*
--------------------
3541511168

(所影响的行数为 1 行)
*/
select dbo.DcodeIP(3541511168)
/*
211.23.44.1

(所影响的行数为 1 行)

*/

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧