0% found this document useful (0 votes)
8 views

SQL Scrips Learn

Uploaded by

nicolubbe20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Scrips Learn

Uploaded by

nicolubbe20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

-- 1.

Check Database Size


SELECT
DB_NAME(database_id) AS DatabaseName,
CONVERT(VARCHAR,SUM(size)*8/1024) + ' MB' AS Size
FROM sys.master_files
WHERE DB_NAME(database_id) NOT IN ('master', 'tempdb', 'model', 'msdb')
GROUP BY database_id
ORDER BY 2 DESC;

-- 2. Check Recent Backups


SELECT
database_name AS DatabaseName,
backup_start_date AS LastBackupDate
FROM msdb.dbo.backupset
WHERE type = 'D' -- D for Database backup
ORDER BY backup_start_date DESC;

-- 3. Check Active Sessions


SELECT
session_id,
login_name,
host_name,
program_name,
status,
cpu_time,
memory_usage
FROM sys.dm_exec_sessions
WHERE is_user_process = 1;

-- 4. Index Fragmentation
SELECT
DB_NAME(database_id) AS DatabaseName,
OBJECT_NAME(object_id) AS TableName,
index_id,
avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'SAMPLED');

-- 5. Long Running Queries


SELECT
s.creation_time,
s.last_execution_time,
s.total_elapsed_time,
s.text
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) s
ORDER BY total_elapsed_time DESC;

-- 6. Check SQL Server Agent Job Status


SELECT
job.name AS JobName,
job.job_id,
run_status,
run_date,
run_time,
msdb.dbo.agent_datetime(run_date, run_time) AS LastRunDateTime
FROM msdb.dbo.sysjobs job
INNER JOIN msdb.dbo.sysjobschedules js ON job.job_id = js.job_id
INNER JOIN msdb.dbo.sysschedules sch ON js.schedule_id = sch.schedule_id;
-- 7. Check for Blocking Sessions
SELECT
blocking_session_id,
wait_type,
resource_type,
resource_description
FROM sys.dm_tran_locks
WHERE blocking_session_id <> 0;

-- 8. List Users and Their Roles


SELECT
p.name AS UserName,
r.name AS RoleName
FROM sys.database_role_members m
INNER JOIN sys.database_principals p ON m.member_principal_id = p.principal_id
INNER JOIN sys.database_principals r ON m.role_principal_id = r.principal_id;

-- 9. Check Failed Logins


SELECT
login_name,
count(*) AS FailedAttempts
FROM sys.dm_exec_connections
WHERE is_successful <> 1
GROUP BY login_name;

-- 10. Check Table Row Counts


EXEC sp_MSforeachtable 'SELECT ''?'' AS TableName, COUNT(*) AS RowCount FROM ?';

.
-- 11. Check Disk Space Usage
EXEC xp_fixeddrives;

-- 12. Find Unused Indexes


SELECT
o.name AS ObjectName,
i.name AS IndexName,
i.index_id,
i.is_disabled,
i.is_hypothetical,
s.user_updates,
s.last_user_update
FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id
LEFT JOIN sys.dm_db_index_usage_stats s ON i.object_id = s.object_id AND i.index_id
= s.index_id
WHERE s.user_updates < 100 AND s.last_user_update IS NOT NULL
ORDER BY s.user_updates, s.last_user_update;

-- 13. Check TempDB Usage


USE tempdb;
EXEC sp_spaceused;

-- 14. List Recently Executed Stored Procedures


SELECT
OBJECT_NAME(objectid) AS ProcedureName,
execution_count,
last_execution_time
FROM sys.dm_exec_procedure_stats
ORDER BY last_execution_time DESC;

-- 15. Check Database Growth


SELECT
name AS DatabaseName,
size * 8 / 1024 AS CurrentSizeMB,
growth * 8 / 1024 AS GrowthMB
FROM sys.master_files;

-- 16. List All Tables with Row Counts


DECLARE @TableName NVARCHAR(255);
DECLARE table_cursor CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';

OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @TableName;

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_executesql N'SELECT '''+@TableName+''' AS TableName, COUNT(*) AS
RowCount FROM '+@TableName;
FETCH NEXT FROM table_cursor INTO @TableName;
END;

CLOSE table_cursor;
DEALLOCATE table_cursor;

-- 17. Check for Orphaned Users


sp_change_users_login 'Report';

-- 18. List Full-Text Index Population Status


SELECT
OBJECT_NAME(object_id) AS TableName,
change_tracking_state_desc,
crawl_type_desc,
crawl_status_desc,
fulltext_catalog_id
FROM sys.fulltext_indexes;

-- 19. Check Database Compatibility Level


SELECT
name AS DatabaseName,
compatibility_level
FROM sys.databases;

-- 20. List Logins and Their Permissions


EXEC sp_msloginmappings;

.
-- 11. Check Disk Space Usage
EXEC xp_fixeddrives;

-- 12. Find Unused Indexes


SELECT
o.name AS ObjectName,
i.name AS IndexName,
i.index_id,
i.is_disabled,
i.is_hypothetical,
s.user_updates,
s.last_user_update
FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id
LEFT JOIN sys.dm_db_index_usage_stats s ON i.object_id = s.object_id AND i.index_id
= s.index_id
WHERE s.user_updates < 100 AND s.last_user_update IS NOT NULL
ORDER BY s.user_updates, s.last_user_update;

-- 13. Check TempDB Usage


USE tempdb;
EXEC sp_spaceused;

-- 14. List Recently Executed Stored Procedures


SELECT
OBJECT_NAME(objectid) AS ProcedureName,
execution_count,
last_execution_time
FROM sys.dm_exec_procedure_stats
ORDER BY last_execution_time DESC;

-- 15. Check Database Growth


SELECT
name AS DatabaseName,
size * 8 / 1024 AS CurrentSizeMB,
growth * 8 / 1024 AS GrowthMB
FROM sys.master_files;

-- 16. List All Tables with Row Counts


DECLARE @TableName NVARCHAR(255);
DECLARE table_cursor CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';

OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @TableName;

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_executesql N'SELECT '''+@TableName+''' AS TableName, COUNT(*) AS
RowCount FROM '+@TableName;
FETCH NEXT FROM table_cursor INTO @TableName;
END;

CLOSE table_cursor;
DEALLOCATE table_cursor;

-- 17. Check for Orphaned Users


sp_change_users_login 'Report';
-- 18. List Full-Text Index Population Status
SELECT
OBJECT_NAME(object_id) AS TableName,
change_tracking_state_desc,
crawl_type_desc,
crawl_status_desc,
fulltext_catalog_id
FROM sys.fulltext_indexes;

-- 19. Check Database Compatibility Level


SELECT
name AS DatabaseName,
compatibility_level
FROM sys.databases;

-- 20. List Logins and Their Permissions


EXEC sp_msloginmappings;

You might also like