Author: Jugal Shah

  • T-SQL Script to check SQL Server Instance TCP/IP Port Number

    SQL Server Error Log

    -- Check the error log for port number
    EXEC xp_ReadErrorLog 0, 1, N'Server is listening on', N'any', NULL, NULL, 'DESC'
    GO
    

    Executing DMV Script

    -- you must connect remotely using TCP/IP
    SELECT local_tcp_port
    FROM   sys.dm_exec_connections
    WHERE  session_id = @@SPID
    GO
    

    For Dynamic Port Number
    — Read the registry for port number

    -- Execute below script if SQL Server is configured with dynamic port number
    DECLARE       @portNo   NVARCHAR(10)
     
    EXEC   xp_instance_regread
    @rootkey    = 'HKEY_LOCAL_MACHINE',
    @key        =
    'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll',
    @value_name = 'TcpDynamicPorts',
    @value      = @portNo OUTPUT
     
    SELECT [PortNumber] = @portNo
    GO
    

    For Static Port Number

    -- Execute below script if SQL Server is configured with static port number
    DECLARE       @portNo   NVARCHAR(10)
     
    EXEC   xp_instance_regread
    @rootkey    = 'HKEY_LOCAL_MACHINE',
    @key        =
    'Software\Microsoft\Microsoft SQL Server\MSSQLServer\SuperSocketNetLib\Tcp\IpAll',
    @value_name = 'TcpPort',
    @value      = @portNo OUTPUT
     
    SELECT [PortNumber] = @portNo
    GO
    
  • Error Fix: Microsoft SQL Server, Error: 14516

    Problem: Proxy (1) is not allowed for subsystem “SSIS” and user “Domain\UserName”. Grant permission by calling sp_grant_proxy_to_subsystem or sp_grant_login_to_proxy. (Microsoft SQL Server, Error: 14516)

    Solution:
    Above error occurs when the user with the minimum permission (i.e. SQLAgentReaderRole and SQLAgentUserRole) or the user is configured as job owner and trying to run the job which is running under the proxy account security context.

    You can execute below script to grant permission to the user and fix the error.

    
    EXEC dbo.sp_grant_login_to_proxy
        @login_name = 'Domain\UserName',
        @proxy_name = 'Proxy Name' ;
    GO
    
  • T-SQL Script to Check if LiteSpeed is installed or not on the server

    You can execute below script against the SQL Server instance and check whether LiteSpeed is installed on the server or not.

    T-SQL script is checking registry to check the LiteSpeed version value.

    declare @regkeyval varchar(20), @value varchar(255), @rc int
    exec @rc=master.dbo.xp_regread 
      @rootkey= 'hkey_local_machine',
      @key='software\imceda\sqllitespeed\engine',
      @value_name='version',
      @regkeyval=@value output
    
    set @value =isnull(@value,'-') 
    
    if @value <> '-'
    select 
     @@servername as SQLInstanceName
    ,case serverproperty('IsClustered') when 1 then cast(serverproperty('computernamephysicalnetbios') as varchar)
     else cast(serverproperty('machinename') as varchar)
     end as WindowsServerName
    ,@value as LiteSpeedVersion 
    ,'LiteSpeed is installed' as LiteSpeedStautsCheck
    else
    select 
     @@servername as SQLInstanceName
    ,case serverproperty('IsClustered') when 1 then cast(serverproperty('computernamephysicalnetbios') as varchar)
     else cast(serverproperty('machinename') as varchar)
     end as WindowsServerName
    ,'LiteSpeed is not installed' as LiteSpeedStautsCheck
    
  • Powershell – To check the Drive and Disk Space Information

    To get the disk space details of the server, simply go to command prompt and paste the below script it will give you disk space details.

    powershell -command "& {Get-WmiObject -Class Win32_LogicalDisk -Filter 'DriveType = 3' |select PSComputerName, Caption,@{N='Capacity_GB'; E={[math]::Round(($_.Size / 1GB), 2)}},@{N='FreeSpace_GB'; E={[math]::Round(($_.FreeSpace / 1GB), 2)}},@{N='PercentUsed'; E={[math]::Round(((($_.Size - $_.FreeSpace) / $_.Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round((($_.FreeSpace / $_.Size) * 100), 2) }}}"
    

    Sample Output
    Caption : C:
    Capacity_GB : 283.44
    FreeSpace_GB : 51.48
    PercentUsed : 81.84
    PercentFree : 18.16

  • T-SQL Script to Check the Native Compressed backup file size and Backup Compression Ratio

    First of all make sure backup compression is enabled on the SQL Server. You can execute below query to check the backup compression.

    select name,[description],value_in_use from sys.configurations where name like '%backup%'
    

    Execute below script to check the compressed backup file size and backup compression ratio.

    Declare @FromDate as datetime
    -- Specify the from date value
    set @FromDate = GETDATE() -1
    
    SELECT  
       CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SQLServerName, 
       msdb.dbo.backupset.database_name,  
       CASE msdb..backupset.type  
           WHEN 'D' THEN 'Database'  
           WHEN 'L' THEN 'Log'  
           WHEN 'I' THEN 'Differential'  
       END AS backup_type,  
       msdb.dbo.backupset.backup_start_date,  
       msdb.dbo.backupset.backup_finish_date, 
       msdb.dbo.backupset.expiration_date, 
       DATEDIFF (SECOND, msdb.dbo.backupset.backup_start_date, msdb.dbo.backupset.backup_finish_date) 'Backup Elapsed Time (sec)',
       msdb.dbo.backupset.compressed_backup_size AS 'Compressed Backup Size in KB',
      (msdb.dbo.backupset.compressed_backup_size/1024/1024) AS 'Compress Backup Size in MB',
       CONVERT (NUMERIC (20,3), (CONVERT (FLOAT, msdb.dbo.backupset.backup_size) /CONVERT (FLOAT, msdb.dbo.backupset.compressed_backup_size))) 'Compression Ratio',
       CASE msdb..backupset.type  
           WHEN 'D' THEN 'Database'  
           WHEN 'L' THEN 'Log'  
       END AS backup_type,  
       msdb.dbo.backupset.backup_size,  
       msdb.dbo.backupmediafamily.logical_device_name,  
       msdb.dbo.backupmediafamily.physical_device_name,   
       msdb.dbo.backupset.name AS backupset_name, 
       msdb.dbo.backupset.description 
    FROM   msdb.dbo.backupmediafamily  
       INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id 
    WHERE 
    CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= @FromDate
    AND msdb.dbo.backupset.backup_size > 0 
    ORDER BY  
       msdb.dbo.backupset.database_name, 
       msdb.dbo.backupset.backup_finish_date