码迷,mamicode.com
首页 > 数据库 > 详细

SQLSERVER 查看服务器IP地址的命令

时间:2019-09-03 09:59:56      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:targe   show   lin   pdb   string   使用   head   exe   line   

今天进行负载均衡的测试的时候 想查询一下数据库相关信息 百度了下 找到解决方案为:

SELECT SERVERNAME = CONVERT(NVARCHAR(128),SERVERPROPERTY(‘SERVERNAME‘)) 
,LOCAL_NET_ADDRESS AS ‘IPAddressOfSQLServer‘
,CLIENT_NET_ADDRESS AS ‘ClientIPAddress‘
 FROM SYS.DM_EXEC_CONNECTIONS WHERE SESSION_ID = @@SPID

感谢原作者的blog

 

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

转载:https://edu.aliyun.com/a/12773

 

获取服务器名称:

SELECT SERVERPROPERTY(‘MachineName‘)
select @@SERVERNAME
select HOST_NAME()

 

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

--开启xp_cmdshell 
exec sp_configure‘show advanced options‘, 1 
reconfigure with override 
exec sp_configure‘xp_cmdshell‘, 1 
reconfigure with override 
exec sp_configure‘show advanced options‘, 0 
reconfigure with override 
go 
 
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 , 
  @pos + 1 , 
  len(@ipline) - @pos)))) as ipaddress from #temp 
drop table #temp 
 
 
set nocount off 
end 
go

 

 

SQLSERVER 查看服务器IP地址的命令

标签:targe   show   lin   pdb   string   使用   head   exe   line   

原文地址:https://www.cnblogs.com/jinanxiaolaohu/p/11445501.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!