Posts

Showing posts from September, 2025

SQLDBA- Get the list of indices on table with columns and include columns

  Below help us to get the list of indices  on  table with columns and include columns also. SELECT QUOTENAME(SCHEMA_NAME(t.schema_id)) AS SchemaName,     QUOTENAME(t.name) AS TableName,     QUOTENAME(i.name) AS IndexName, i.type_desc,     i.is_primary_key,     i.is_unique,     i.is_unique_constraint,     STUFF(REPLACE(REPLACE((         SELECT QUOTENAME(c.name) + CASE WHEN ic.is_descending_key = 1 THEN ' DESC' ELSE '' END AS [data()]         FROM sys.index_columns AS ic         INNER JOIN sys.columns AS c ON ic.object_id = c.object_id AND ic.column_id = c.column_id         WHERE ic.object_id = i.object_id AND ic.index_id = i.index_id AND ic.is_included_column = 0         ORDER BY ic.key_ordinal         FOR XML PATH     ), '<row>', ', '), '</row>', ''), 1, 2, '') AS KeyC...

SQL DBA - Find the no of databases base, no of tables each database and no of column each table

We got requirement  how many no of database in each instance ,  how many  no of tables each database and how many  no of columns in each table.  Below queries  useful to get the above information.  1. Below queries will return with multiple result set  with above information.   --Number of Databases in the Cluster (Production Only) SELECT COUNT(*) AS NumberOfDatabases FROM sys.databases WHERE name NOT IN ('master','tempdb','model','msdb','staging' );  -- Adjust this filter based on your naming convention --SELECT * FROM sys.databases --2. Number of Tables Within Each Database DECLARE @sql NVARCHAR(MAX) = ''; SELECT @sql += ' USE [' + name + ']; SELECT ''' + name + ''' AS DatabaseName, COUNT(*) AS TableCount FROM sys.tables; ' FROM sys.databases WHERE name NOT IN ('master','tempdb','model','msdb','staging' ) AND state_desc = 'ONLINE'; EXEC sp_executesql...