• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何通过T-SQL获得当前连接的客户端的I_sqlserver

sqlserver 搞代码 7年前 (2018-06-16) 144次浏览 已收录 0个评论

/*****************************************************************************************************************

    下面的SP是返回所有的客户端的IP和HOSTNAME,目的是可以通过JOB返回某一时间点的CLIENT 的连接情况.

     我当时写这个脚本的目的是经常有一些没有授权的客户机,通过sqlserver的CLIENT就连接到SQLSERVER,所以我可以定义一个JOB每隔30分钟运行一次这个存储过程,并且将内容写的一个LOG文件,这样可以大概记录有哪些CLIENT在连接SQLSERVER,当然大家可以可以修改这个脚本,使之返回更多的信息,比如CPU,MEMORY,LOCK….

http://www.gaodaima.com/35351.html如何通过T-SQL获得当前连接的客户端的I_sqlserver

Author:黄山光明顶

mail:[email protected]

version:1.0.0

date:2004-1-30

(如需转载,请注明出处!)

*********************************************************************************************************/

Create proc usp_getClient_infor
as
set nocount on

Declare @rc int
Declare @RowCount int

Select @rc=0
Select @RowCount=0

begin
–//create temp table ,save sp_who information
  create table #tspid(
 spid int null,
 ecid int null,
 status nchar(60) null,
 loginname nchar(256) null,
 hostname nchar(256) null,
 blk bit null,
 dbname nchar(256) null,
 cmd nchar(32)
  )

–//create temp table save all SQL client IP and hostname and login time
Create table #userip(
 [id]int identity(1,1),
 txt varchar(1000),
)

–//Create result table to return recordset
Create table #result(
 [id]int identity(1,1),
 ClientIP varchar(1000),
 hostname nchar(256),
 login_time datetime default(getdate())

 )
–//get host name by exec sp_who ,insert #tspid from sp_who,
insert into #tspid(spid,ecid,status,loginname,hostname,blk,dbname,cmd) exec sp_who

declare @cmdStr varchar(100),
 @hostName nchar(256),
 @userip varchar(20),
 @sendstr varchar(100)
 

–//declare a cursor from table #tspid
declare tspid cursor
for select distinct hostname from #tspid  with (nolock) where spid>50
for read only

open tspid
   fetch next from tspid into @hostname
   While @@FETCH_STATUS = 0
   begin
 select @cmdStr=’ping ‘+rtrim(@hostName)
 
 insert into #userip(txt) exec master..xp_cmdshell @cmdStr
 
 select @rowcount=count(id) from #userIP

 if @RowCount=2 –//no IP feedback package
  begin
  insert into #Result(ClientIP,hostname) values(‘Can not get feedback package from Ping!’,@hostname)
  end
 if @RowCount>2 
  begin
  select @userip=substring(txt,charindex(‘[‘,txt)+1,charindex(‘]’,txt)-charindex(‘[‘,txt)-1)
  from #userIP
  where txt like ‘Pinging%’
  
  insert into #Result(ClientIP,hostname) values(@userIP,@hostname)
  end
 select @rc=@@error
 if @rc=0
  truncate table #userip –//clear #userIP table

   fetch next from tspid into @hostname
 end

close tspid
deallocate tspid

select * from #result with(nolock)

drop table #tspid
drop table #userip
drop table #result
end
go
exec usp_getClient_infor

欢迎大家阅读《如何通过T-SQL获得当前连接的客户端的I_sqlserver,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何通过T-SQL获得当前连接的客户端的I_sqlserver

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址