與服務器相連的才能列出來sp_who

解决方案 »

  1.   

    连上一个服务器,执行
    1,exec master..xp_cmdshell'osql -L'
    2,或者
    declare @cmdStr varchar(100)
    create table #table(id int identity,txt varchar(1000))
    create table #t(hostname varchar(100), ip varchar(100))
    declare @id int
    declare @hostname varchar(100)
    set @id = 50
    set nocount on
    while exists (select top 1 HostName from master..sysprocesses where spid > @id)
    begin
        select top 1 @cmdStr='ping '+HostName, @id = spid, @hostname = hostname from master..sysprocesses where spid > @id order by spid 
        insert into #table(txt) exec master..xp_cmdshell @cmdStr
        insert #T select @hostname, substring(txt,charindex('[',txt)+1,charindex(']',txt)-charindex('[',txt)-1) from #table where id=2
        truncate table #table
    end
    set nocount off
    select distinct * from #t
    drop table #table
    drop table #t
      

  2.   

    D:\Home\Ncowboy>osql -L服务器:
        (local)
        LACL-006-W2S
        LACL_EXPERIMENTD:\Home\Ncowboy>
    D:\Home\Ncowboy>osql -E
    1> declare @cmdStr varchar(100)
    2> create table #table(id int identity,txt varchar(1000))
    3> create table #t(hostname varchar(100), ip varchar(100))
    4> declare @id int
    5> declare @hostname varchar(100)
    6> set @id = 50
    7> set nocount on
    8> while exists (select top 1 HostName from master..sysprocesses where spid > @i
    d)
    9> begin
    10>     select top 1 @cmdStr='ping '+HostName, @id = spid, @hostname = hostname
    from master..sysprocesses where spid > @id order by spid
    11>     insert into #table(txt) exec master..xp_cmdshell @cmdStr
    12>     insert #T select @hostname, substring(txt,charindex('[',txt)+1,charindex
    (']',txt)-charindex('[',txt)-1) from #table where id=2
    13>     truncate table #table
    14> end
    15> set nocount off
    16> select distinct * from #t
    17> drop table #table
    18> drop table #t
    19> go
     hostname
            ip
     -------------------------------------------------------------------------------        ---------------------
            ------------------------------------------------------------------------        ----------------------------
     LACL-008
            192.168.103.8(1 行受到影响)
    1>
      

  3.   

    楼上,我已经说了,我这儿没有安装客户端和服务器端,何来osql???我现在想在程序中实现,用udl文件是可以设置的,也就是说理论上是可以办到的,但是不知道在程序中怎么写。
      

  4.   

    用ado存储过程:sp_who或者 select * from sysprocesses都可以