当前位置: 首页 > news >正文

获取SQL Server服务器的连接信息用脚本(在原邹建写的基础上作一点改进)

--获取SQL Server服务器的连接信息用脚本(在原邹建写的基础上作一点改进)

declare
@dbname sysname,
--要查询的数据库名(为空为所有),默认查询所有数据库的连接信息
@includeip bit
--是否显示IP地址(0否,1是),因为查询IP地址比较费时,所以增加此控制

select @dbname=null,@includeip=1

declare @dbid int
set @dbid=db_id(@dbname)
create table #tb
(id int identity(1,1),dbname sysname,hostname nchar(128),loginname nchar(128),
net_address nchar(12),net_ip nvarchar(15),prog_name nchar(128))
insert into #tb(hostname,dbname,net_address,loginname,prog_name)
select distinct hostname,db_name(dbid),net_address,loginame,program_name
from master..sysprocesses
where hostname<>'' and (@dbid is null or dbid=@dbid)
if @includeip=0 goto lb_show
--如果不显示IP地址,就直接显示
declare @sql varchar(500),@hostname nchar(128),@id int
create table #ip(hostname nchar(128),a varchar(200))
declare tb cursor local for select distinct hostname from #tb
open tb
fetch next from tb into @hostname
while @@fetch_status=0
begin
set @sql='ping '+@hostname+' -a -n 1 -l 1'
insert #ip(a) exec master..xp_cmdshell @sql
update #ip set hostname=@hostname where hostname is null
fetch next from tb into @hostname
end
update #tb set net_ip=left(a,patindex('%:%',a)-1)
from #tb a inner join (
select hostname,a=substring(a,patindex('Ping statistics for %:%',a)+20,20)
from #ip
where a like 'Ping statistics for %:%') b on a.hostname=b.hostname
drop table #ip

lb_show:
select id,数据库名=dbname,客户机名=hostname,用户名=loginname
,网卡物理地址=net_address,IP地址=net_ip,应用程序名称=prog_name
from #tb

drop table #tb
--查询结果:

------------------------------------------------------------------------------------------------------------

相关文章:

  • Agile实践日志(2)-- Daily Stand up 和 Retrospective Session
  • 一短查询SQL Server服务器阻塞和死锁信息用的脚本
  • VB.Net 2005 WinFrom 文件上传程序...
  • 论道谈谈
  • WinDBG 技巧:设断点命令详解(bp, bu, bm, ba 以及bl, bc, bd, be)
  • Auto Complete -- Typeahead 和DateRangePicker
  • 有多少域名被漏掉了?
  • C# Windows Azure Queue的操作
  • 移动设备管理(MDM)与OMA(OTA)DM协议向导(二)——WAP协议(1)
  • Submit disabled Dropdown
  • Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
  • [Windows编程] 如何判断操作系统是64位还是32位
  • 使用Roslyn动态编译和执行
  • 利用Windows7内置功能管理虚拟磁盘
  • 使用cecil 完成 code injection
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • bootstrap创建登录注册页面
  • gulp 教程
  • java8-模拟hadoop
  • Mysql优化
  • vue自定义指令实现v-tap插件
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 类orAPI - 收藏集 - 掘金
  • 理清楚Vue的结构
  • 面试遇到的一些题
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 试着探索高并发下的系统架构面貌
  • 为视图添加丝滑的水波纹
  • hi-nginx-1.3.4编译安装
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​香农与信息论三大定律
  • ( 10 )MySQL中的外键
  • (02)vite环境变量配置
  • (pojstep1.3.1)1017(构造法模拟)
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (七)理解angular中的module和injector,即依赖注入
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (五)MySQL的备份及恢复
  • (一)80c52学习之旅-起始篇
  • (转)重识new
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .Net CoreRabbitMQ消息存储可靠机制
  • .NET Core跨平台微服务学习资源
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .NET Standard 的管理策略
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .net6 webapi log4net完整配置使用流程
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .net打印*三角形
  • ::before和::after 常见的用法
  • @EventListener注解使用说明
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • @NoArgsConstructor和@AllArgsConstructor,@Builder