Tuesday, April 11, 2017

how to capture a host Ip address through T-SQL

We can capture host address of a server through T_SQL using below script. 

DECLARE @ipaddress varchar(max)
DECLARE @ipaddressLine varchar(max)
DECLARE @pos int

SET nocount on
SET @ipaddress = NULL

Create table #temp_ip (ipLine varchar(200))
INSERT #temp_ip EXEC master..xp_cmdshell 'ipconfig'

SELECT top 1 @ipaddressLine = ipLine FROM #temp_ip
WHERE upper (ipLine) like '%IPV4 ADDRESS%' or upper (ipLine) like '%IP ADDRESS%'
    IF (isnull (@ipaddressLine,'***') != '***')
      BEGIN
       SET @pos = CharIndex (':',@ipaddressLine,1);
       SET @ipaddress = rtrim(ltrim(substring (@ipaddressLine , @pos + 1 ,len (@ipaddressLine) - @pos)))
      END
PRINT @ipaddress
DROP TABLE #temp_ip

SET nocount off

All Database Sizes in SQL SERVER

DB sizes  SELECT d.name, (ROUND(SUM(cast(mf.size as bigint)) * 8 / 1024, 0))/1024 Size_GB FROM sys.master_files mf ...