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

4 comments:

  1. Query to Find out the XP_CommandShell Status

    SELECT case CONVERT(INT, ISNULL(value, value_in_use)) when 0 then 'xp_cmdshell is disabled'
    when 1 then 'xp_cmdshell is enabled' else 'null' end
    FROM sys.configurations
    WHERE name = 'xp_cmdshell' ;

    ReplyDelete
  2. To Add Row Values in a Single Column..

    declare @dblist nvarchar(max)
    IF OBJECT_ID('tempdb..#temp_TLogDBList') IS NOT NULL DROP TABLE #temp_TLogDBList;
    Create table #temp_TLogDBList (Names nvarchar(max))
    insert into #temp_TLogDBList select 'MSSQL:' + name from master..sysdatabases where databasepropertyex(name,'Recovery') ='FULL' and DATABASEPROPERTYEX(name,'Status') = 'ONLINE'
    and name not in('master','model','msdb','tempdb')
    --select * from #temp_TLogDBList
    SELECT @dblist =Stuff(
    (SELECT N' ' + Names FROM #temp_TLogDBList FOR XML PATH(''),TYPE)
    .value('text()[1]','nvarchar(max)'),1,2,N'')
    print @dblist

    ReplyDelete
  3. declare @dblist nvarchar(max)
    IF OBJECT_ID('tempdb..#temp_TLogDBList') IS NOT NULL DROP TABLE #temp_TLogDBList;
    Create table #temp_TLogDBList (Names nvarchar(max))
    insert into #temp_TLogDBList select 'MSSQL:' + name from master..sysdatabases where databasepropertyex(name,'Recovery') ='FULL' and DATABASEPROPERTYEX(name,'Status') = 'ONLINE'
    and name not in('master','model','msdb','tempdb')
    --select * from #temp_TLogDBList
    SELECT @dblist =Stuff(
    (SELECT N' ' + Names FROM #temp_TLogDBList FOR XML PATH(''),TYPE)
    .value('text()[1]','nvarchar(max)'),1,2,N'')
    print @dblist

    ReplyDelete
  4. Adding Row Values in a Single Column..

    declare @dblist nvarchar(max)
    IF OBJECT_ID('tempdb..#temp_TLogDBList') IS NOT NULL DROP TABLE #temp_TLogDBList;
    Create table #temp_TLogDBList (Names nvarchar(max))
    insert into #temp_TLogDBList select 'MSSQL:' + name from master..sysdatabases where databasepropertyex(name,'Recovery') ='FULL' and DATABASEPROPERTYEX(name,'Status') = 'ONLINE'
    and name not in('master','model','msdb','tempdb')
    --select * from #temp_TLogDBList
    SELECT @dblist =Stuff(
    (SELECT N' ' + Names FROM #temp_TLogDBList FOR XML PATH(''),TYPE)
    .value('text()[1]','nvarchar(max)'),1,2,N'')
    print @dblist

    ReplyDelete

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 ...