此过程是为了返回本机IP地址,但有的返回空值,不知为什么?

sailor_2000 2006-02-28 11:24:08
create PROCEDURE dbo.GetLocalIP @IPHZH varchar(30) output AS

set xact_abort on
begin

create table #ip(id int identity(1,1),re varchar(2000))

declare @s varchar(1000),@pos int
set @s = 'ping ' + host_name() + ' -a -n 1 -l 1'
insert #ip(re) exec master..xp_cmdshell @s

select @IPHZH=stuff(left(re,charindex(']',re)-1),1,charindex('[',re),'') from #ip where id = 2

select @pos=0
while 1=1
begin
select @IPHZH=substring(@IPHZH,PATINDEX('%.%',@IPHZH)+1,len(@IPHZH)-PATINDEX('%.%',@IPHZH))
if @pos>0 break
select @pos=@pos+1
end
select @IPHZH='_'+STUFF(@IPHZH, PATINDEX('%.%',@IPHZH), 1, '_')

drop table #ip
end
...全文
127 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlp321002 2006-02-28
  • 打赏
  • 举报
回复
--你是不是截取IP地址字符串有问题。
--你用下邹建的试试

declare @ip varchar(15),@sql varchar(1000)

--得到ip地址
create table #ip(a varchar(200))
set @sql='ping '+host_name()+' -a -n 1 -l 1'
insert into #ip exec master..xp_cmdshell @sql

select @ip=left(a,patindex('%:%',a)-1) from(
select a=substring(a,patindex('Ping statistics for %:%',a)+20,20)
from #ip where a like 'Ping statistics for %:%') a

--显示结果
select 用户计算机名=host_name(),ip地址=@ip

drop table #ip
子陌红尘 2006-02-28
  • 打赏
  • 举报
回复
可能性比较多,比如服务器禁止了被PING的功能

22,300

社区成员

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

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