--创建存储过程
create procedure getip
@ip varchar(50) output
asdeclare @cmd varchar(100)
set @cmd='ping '+host_name()
create table #(s varchar(200))
insert # exec master..xp_cmdshell @cmd
select @ip=s from # where ltrim(s) like 'Pinging%'
set @ip=substring(@ip,charindex('[',@ip)+1,charindex(']',@ip)-charindex('[',@ip)-1)
drop table #
go--调用
declare @ip varchar(100)
exec p @ip output
select @ip

解决方案 »

  1.   

    如果要获取非直接连接SQL Server的客户端的IP地址,需要在应用程序中获取
      

  2.   

    如果要获取当前系统所有客户端的IP地址,可以在sysprocesses中先获取hostname,然后查询iP获取hostname:
    select hostname from master..sysprocesses where hostname<>''获取IP:--创建存储过程
    create procedure getip
    @hostname varchar(20),
    @ip varchar(50) output
    asdeclare @cmd varchar(100)
    set @cmd='ping '+@hostname
    create table #(s varchar(200))
    insert # exec master..xp_cmdshell @cmd
    select @ip=s from # where ltrim(s) like 'Pinging%'
    set @ip=substring(@ip,charindex('[',@ip)+1,charindex(']',@ip)-charindex('[',@ip)-1)
    drop table #
    go
      

  3.   

    哇, vivianfdlpw()  你的代码 获得 IP 时 ,执行速度很慢呀, 要3秒的时间!!
      

  4.   

    =======================================================================================
    /*获得计算机名称和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 @sqlselect @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地址=@ipdrop table #ip
    =============================================================================================create table #(txt varchar(200))
    insert # exec master..xp_cmdshell 'ipconfig /all'select * from # where ltrim(txt) like 'host name%' or ltrim(txt) like 'ip address%'drop table #--结果
    /*
    txt                                                 
    ------------------------------------------------------------
       Host Name . . . . . . . . . . . . : Lydia   IP Address. . . . . . . . . . . . : 192.168.18.188   IP Address. . . . . . . . . . . . : 192.168.18.168   IP Address. . . . . . . . . . . . : 192.168.18.18
    (所影响的行数为 4 行)
    */
      

  5.   

    vivianfdlpw() 和 zlp321002(想在北京找份工作!)是同理,我觉得是目前比较好的方法