Showing posts with label TSQL. Show all posts
Showing posts with label TSQL. Show all posts

Monday, November 21, 2016

T-SQL code to get the active Connections for each Database.

--Method 1
 SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NoOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
    dbid > 0
GROUP BY 
    dbid, loginame


--Method 2
EXEC sp_who2


--Method 3
SELECT  spid,
        sp.[status],
        loginame [Login],
        hostname, 
        blocked BlkBy,
        sd.name DBName, 
        cmd Command,
        cpu CPUTime,
        physical_io DiskIO,
        last_batch LastBatch,
        [program_name] ProgramName   
FROM master.dbo.sysprocesses sp 
JOIN master.dbo.sysdatabases sd ON sp.dbid = sd.dbid
ORDER BY DBName,spid

Tuesday, September 23, 2014

Script to find Windows authentication mode/ Mixed authentication mode in SQL Server using TSQL

DECLARE @AuthenticationMode INT

EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE'
 ,N'Software\Microsoft\MSSQLServer\MSSQLServer'
 ,N'LoginMode'
 ,@AuthenticationMode OUTPUT

SELECT CASE @AuthenticationMode
  WHEN 1
   THEN 'Windows Authentication'
  WHEN 2
   THEN 'Windows and SQL Server Authentication'
  ELSE 'Unknown'
  END AS [Authentication Mode]