Retrieve Information about your Instance
Retrieve Local and Remote Servers
Section titled “Retrieve Local and Remote Servers”To retrieve a list of all servers registered on the instance:
EXEC sp_helpserver;Get information on current sessions and query executions
Section titled “Get information on current sessions and query executions”sp_who2This procedure can be used to find information on current SQL server sessions. Since it is a procedure, it’s often helpful to store the results into a temporary table or table variable so one can order, filter, and transform the results as needed.
The below can be used for a queryable version of sp_who2:
-- Create a variable table to hold the results of sp_who2 for querying purposes
DECLARE @who2 TABLE ( SPID INT NULL, Status VARCHAR(1000) NULL, Login SYSNAME NULL, HostName SYSNAME NULL, BlkBy SYSNAME NULL, DBName SYSNAME NULL, Command VARCHAR(8000) NULL, CPUTime INT NULL, DiskIO INT NULL, LastBatch VARCHAR(250) NULL, ProgramName VARCHAR(250) NULL, SPID2 INT NULL, -- a second SPID for some reason...? REQUESTID INT NULL)
INSERT INTO @who2EXEC sp_who2
SELECT *FROM @who2 wWHERE 1=1Examples:
-- Find specific user sessions:SELECT *FROM @who2 wWHERE 1=1 and login = 'userName'
-- Find longest CPUTime queries:SELECT top 5 *FROM @who2 wWHERE 1=1order by CPUTime descRetrieve Edition and Version of Instance
Section titled “Retrieve Edition and Version of Instance”SELECT SERVERPROPERTY('ProductVersion') AS ProductVersion, SERVERPROPERTY('ProductLevel') AS ProductLevel, SERVERPROPERTY('Edition') AS Edition, SERVERPROPERTY('EngineEdition') AS EngineEdition;Retrieve Instance Uptime in Days
Section titled “Retrieve Instance Uptime in Days”SELECT DATEDIFF(DAY, login_time, getdate()) UpDaysFROM master..sysprocessesWHERE spid = 1Information about SQL Server version
Section titled “Information about SQL Server version”To discover SQL Server’s edition, product level and version number as well as the host machine name and the server type:
SELECT SERVERPROPERTY('MachineName') AS Host, SERVERPROPERTY('InstanceName') AS Instance, DB_NAME() AS DatabaseContext, SERVERPROPERTY('Edition') AS Edition, SERVERPROPERTY('ProductLevel') AS ProductLevel, CASE SERVERPROPERTY('IsClustered') WHEN 1 THEN 'CLUSTERED' ELSE 'STANDALONE' END AS ServerType, @@VERSION AS VersionNumber;General Information about Databases, Tables, Stored procedures and how to search them.
Section titled “General Information about Databases, Tables, Stored procedures and how to search them.”Query to search last executed sp’s in db
SELECT execquery.last_execution_time AS [Date Time], execsql.text AS [Script]FROM sys.dm_exec_query_stats AS execqueryCROSS APPLY sys.dm_exec_sql_text(execquery.sql_handle) AS execsqlORDER BY execquery.last_execution_time DESCQuery to search through Stored procedures
SELECT o.type_desc AS ROUTINE_TYPE,o.[name] AS ROUTINE_NAME,m.definition AS ROUTINE_DEFINITIONFROM sys.sql_modules AS m INNER JOIN sys.objects AS oON m.object_id = o.object_id WHERE m.definition LIKE '%Keyword%'order by ROUTINE_NAMEQuery to Find Column From All Tables of Database
SELECT t.name AS table_name,SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_nameFROM sys.tables AS tINNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_IDwhere c.name like 'Keyword%'ORDER BY schema_name, table_name;Query to to check restore details
WITH LastRestores AS(SELECT DatabaseName = [d].[name] , [d].[create_date] , [d].[compatibility_level] , [d].[collation_name] , r.*, RowNum = ROW_NUMBER() OVER (PARTITION BY d.Name ORDER BY r.[restore_date] DESC)FROM master.sys.databases dLEFT OUTER JOIN msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.Name)SELECT *FROM [LastRestores]WHERE [RowNum] = 1Query to to find the log
select top 100 * from databaselogOrder by Posttime descQuery to to check the Sps details
SELECT name, create_date, modify_dateFROM sys.objectsWHERE type = 'P'Order by modify_date desc