加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > MsSql > 正文

SQL语句实现查询SQL Server服务器名称和IP地址

发布时间:2020-05-27 13:14:09 所属栏目:MsSql 来源:互联网
导读:这篇文章主要介绍了SQL语句实现查询SQL Server服务器名称和IP地址,本文分别给出查询语句,需要的朋友可以参考下

获取服务器名称:

获取IP地址可以使用xp_cmdshell执行ipconfig命令:

begin
declare @ipline varchar(200)
declare @pos int
declare @ip varchar(40)
set nocount on
set @ip = null
if object_id('tempdb..#temp') is not null drop table #temp
create table #temp(ipline varchar(200))
insert #temp exec master..xp_cmdshell'ipconfig'
select @ipline = ipline
from #temp
where upper(ipline) like '%IPv4 地址%'--这里需要注意一下,系统不同这里的匹配值就不同
if @ipline is not null
begin
set @pos = charindex(':',@ipline,1);
set @ip = rtrim(ltrim(substring(@ipline,@pos + 1,len(@ipline) - @pos)))
end
select distinct(rtrim(ltrim(substring(@ipline,len(@ipline) - @pos)))) as ipaddress from #temp
drop table #temp

set nocount off
end
go

但是很多情况下由于安全问题是不允许使用xp_cmdshell,可以通过查询SYS.DM_EXEC_CONNECTIONS :


(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读