Display users using sp_who, sp_who2 – SQL Server

-- see users logged - table variable example 
DECLARE @whotable TABLE
(
   SPID INT,  
   Status VARCHAR(1000) NULL,  
   Login SYSNAME NULL,  
   HostName SYSNAME NULL,  
   BlkBy SYSNAME NULL,  
   DBName SYSNAME NULL,  
   Command VARCHAR(1000) NULL,  
   CPUTime INT NULL,  
   DiskIO INT NULL,  
   LastBatch VARCHAR(1000) NULL,  
   ProgramName VARCHAR(1000) NULL,  
   SPID2 INT,
   REQUESTID INT
)

INSERT INTO @whotable EXEC sp_who2

select * from @whotable
-- see users logged - temp table example
CREATE TABLE #sp_who2 
(
   SPID INT,  
   Status VARCHAR(1000) NULL,  
   Login SYSNAME NULL,  
   HostName SYSNAME NULL,  
   BlkBy SYSNAME NULL,  
   DBName SYSNAME NULL,  
   Command VARCHAR(1000) NULL,  
   CPUTime INT NULL,  
   DiskIO INT NULL,  
   LastBatch VARCHAR(1000) NULL,  
   ProgramName VARCHAR(1000) NULL,  
   SPID2 INT,
   REQUESTID INT
) 
GO

INSERT INTO #sp_who2
EXEC sp_who2
GO

SELECT *
FROM #sp_who2
GO

DROP TABLE #sp_who2
GO

Display Recently-executed SQL – SQL Server

SELECT * FROM
(
SELECT TOP 10000 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1) AS [Text],
qs.execution_count,
qs.total_logical_reads, qs.last_logical_reads,
qs.total_logical_writes, qs.last_logical_writes,
qs.total_worker_time/1000 total_worker_time_in_ms,
qs.last_worker_time/1000 last_worker_time_in_ms,
qs.total_elapsed_time/1000 total_elapsed_time_in_ms,
qs.last_elapsed_time/1000 last_elapsed_time_in_ms,
qs.last_execution_time,
qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.last_execution_time DESC
) T

Get Size of all tables in a SQL Server Database

use [<dbname>]
go

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    t.Name

Launch Admin Tools from Windows Command Line

Many of the Windows admin tools can be quickly accessed by launching them from an administrator command prompt.

AD Domains and Trusts

domain.msc

Active Directory Management

admgmt.msc

AD Sites and Serrvices

dssite.msc

AD Users and Computers

dsa.msc

ADSI Edit

adsiedit.msc

Authorization Manager

azman.msc

Certification Authority Management

certsrv.msc

Certificate Templates

certtmpl.msc

Cluster Administrator

cluadmin.exe

Computer Management

compmgmt.msc

Component Services

comexp.msc

Configure Your Server

cys.exe

Device Manager

devmgmt.msc

DHCP Managment

dhcpmgmt.msc

Disk Defragmenter

dfrg.msc

Disk Manager

diskmgmt.msc

Distributed File System

dfsgui.msc

DNS Managment

dnsmgmt.msc

Event Viewer

eventvwr.msc

Indexing Service Management

ciadv.msc

IP Address Manage

ipaddrmgmt.msc

Licensing Manager

llsmgr.exe

Local Certificates Management

certmgr.msc

Local Group Policy Editor

gpedit.msc

Local Security Settings Manager

secpol.msc

Local Users and Groups Manager

lusrmgr.msc

Network Load balancing

nlbmgr.exe

Performance Montior

perfmon.msc

PKI Viewer

pkiview.msc

Public Key Managment

pkmgmt.msc

QoS Control Management

acssnap.msc

Remote Desktops

tsmmc.msc

Remote Storage Administration

rsadmin.msc

Removable Storage

ntmsmgr.msc

Removalbe Storage Operator Requests

ntmsoprq.msc

Routing and Remote Access Manager

rrasmgmt.msc

Resultant Set of Policy

rsop.msc

Schema management

schmmgmt.msc

Services Management

services.msc

Shared Folders

fsmgmt.msc

SID Security Migration

sidwalk.msc

Telephony Management

tapimgmt.msc

Terminal Server Configuration

tscc.msc

Terminal Server Licensing

licmgr.exe

Terminal Server Manager

tsadmin.exe

UDDI Services Managment

uddi.msc

Windows Mangement Instumentation

wmimgmt.msc

WINS Server manager

winsmgmt.msc

Windows Firewall with Advanced Security on Local Computer

wf.msc

Moving/Migrating a SQL Server Database

When migrating or moving a database from one instance to another, be careful with logins and database owners. Ideally, all logins, principals etc. on the source instance should be present on the new instance. after the move, ensure that the new databases all have valid owners. If the database owner changes, privileges on database objects may cause stored procedures and other processes to malfunction. This can also cause many errors in the SQL Server logs and lead to disk full conditions.