List of All Databases in SQL Server
Introduction
When you need to list of all databases in SQL Server, whether you are a database administrator, a developer, or a data analyst, knowing exactly which databases exist on an instance is the first step toward effective management, troubleshooting, and optimization. This article provides a complete walkthrough that explains how to retrieve the complete list of databases, describes the system‑level databases that ship with SQL Server, and offers practical scripts and tips for everyday use. By following the steps outlined below, you will be able to generate an accurate inventory of every database—both system and user—directly from T‑SQL or through the graphical interface, ensuring you never miss a critical data store again Not complicated — just consistent..
Steps to Retrieve the Complete List of Databases
1. Use the sys.databases Catalog View
The most reliable and straightforward method is to query the built‑in catalog view sys.Day to day, databases. This view contains one row per database and provides key columns such as name, database_id, state_desc, and recovery_model_desc.
SELECT
name,
database_id,
state_desc,
recovery_model_desc,
compatibility_level,
create_date,
owner_sid
FROM sys.databases
ORDER BY name;
Why this works: sys.databases is maintained by the SQL Server engine and reflects the current state of every database, including temporary databases that may be created and dropped dynamically Easy to understand, harder to ignore..
2. Query the sys.databases View with Additional Filters
If you only want user databases (excluding the four system databases), add a WHERE clause:
SELECT name, state_desc
FROM sys.databases
WHERE database_id > 4 -- 4 = master, model, msdb, tempdb
ORDER BY name;
3. Use the sp_databases Stored Procedure
For a quick, legacy‑style output, the system stored procedure sp_databases can be executed:
EXEC sp_databases;
Note: sp_databases returns a result set with the same columns as sys.databases, but it is maintained for backward compatibility and may be removed in future versions But it adds up..
4. Retrieve Databases via SQL Server Management Studio (SSMS)
While not a T‑SQL method, SSMS provides a visual way to list databases:
- Connect to the SQL Server instance in Object Explorer.
- Expand the Databases node.
- The full list appears, and you can right‑click any database to view properties.
This approach is handy for ad‑hoc checks but does not script easily, so developers often prefer the T‑SQL options above.
5. Query the information_schema Views
For those who prefer ANSI‑standard views, the information_schema provides a BASE_TABLES or TABLES view, but to get a full database list you still need sys.databases. On the flip side, you can join information_schema.schemata to get per‑database object counts if needed.
Scientific Explanation: How SQL Server Stores Database Metadata
SQL Server maintains metadata about each database in system catalog views located in the master database. databases**, is populated by the engine during database creation and updated automatically when a database is altered (e.The primary view, **sys.Still, g. , when a file is added, removed, or when the state changes).
The official docs gloss over this. That's a mistake Not complicated — just consistent..
database_id: An internal numeric identifier unique to the instance. System databases always have IDs 1‑4.state_desc: Indicates the current state (e.g.,ONLINE,RESTORING,RECOVERY_PENDING).recovery_model_desc: Describes the recovery model (FULL,BULK_LOGGED,SIMPLE).create_date: Timestamp of when the database was created.
These columns are stored in the system tables (sysobjects, sys.databases) that are hidden from direct user access but are exposed through the catalog views. Because the engine updates these tables within the same transaction that creates or modifies a database, the information is always consistent with the current server state.
Understanding this mechanism helps you trust the results of sys.That said, g. That's why databases and avoid relying on ad‑hoc queries that might miss a database due to timing issues (e. , a database that was just created and not yet visible to other sessions).
Frequently Asked Questions (FAQ)
Q1: Does the list include temporary databases created by stored procedures?
A: Yes. Temporary databases such as #temp (local) and ##temp (global) appear in sys.databases with names starting with # or ##. They are automatically dropped when the session that created them ends, but they remain visible until that moment Took long enough..
Q2: How can I filter out system databases in a single query?
A: Use the condition WHERE database_id > 4. This excludes the four built‑in databases (master, model, msdb, tempdb).
Q3: Can I retrieve the list of databases from a remote server?
A: Absolutely. Connect to the remote instance in SSMS or execute the same T‑SQL query on the remote server. If you need a centralized inventory, you can use linked servers or PowerShell scripts that run the query on multiple instances Most people skip this — try not to. But it adds up..
Q4: What about databases that are offline or in recovery?
A: The state_desc column will show OFFLINE, RESTORING, RECOVERY_PENDING, etc. These states are important for health checks and for deciding whether a database requires attention And that's really what it comes down to..
Q5: Is there a way to export the database list to a file?
A: Yes. You can use sqlcmd or SSMS’s “Results to Text” option and redirect the output, or simply run the query inside a stored procedure that writes the result to a table or a CSV file using OPENROWSET or bcp.
Beyond the basic SELECT * FROM sys.databases statement, administrators can extend its use in a number of productive directions.
Security and permission considerations
To query the catalog view you need the VIEW ANY DATABASE permission or membership in the db_securityadmin fixed server role. If you are limited to a narrower scope, you can still retrieve information about databases you own by joining sys.databases to sys.databases → owner_sid and then filtering on the corresponding login’s permissions. Granting the broader permission solely for inventory purposes is often unnecessary; a well‑crafted query that respects the caller’s context can deliver the same result set while keeping the security surface minimal.
Customizing the result set
The default list returns every database, including system objects, temporary tables, and databases that are currently offline. In many scenarios you will want to exclude or highlight specific subsets:
- Exclude system databases –
WHERE database_id > 4(as shown in the FAQ) orWHERE name NOT LIKE 'master%'for a more explicit filter. - Show only online databases –
WHERE state_desc = 'ONLINE'. This is useful when you are performing health‑checks that should ignore databases still being restored or recovering. - Flag recovery models – adding
recovery_model_descto the SELECT list lets you spot databases that run under BULK_LOGGED or SIMPLE, which may require special backup strategies.
Automating the inventory
- SQL Server Agent jobs – schedule a job that runs the query and writes the result to a history table, a CSV file via
bcp, or an email attachment. The job can be set to run nightly, weekly, or after any major topology change. - PowerShell / SMO – using the SQL Server Management Objects library, a short script can connect to multiple instances, execute the same query, and aggregate the results into a single spreadsheet or JSON document. This approach is especially handy for large estates where a single server view does not provide a holistic picture.
- Linked servers – if you have a central reporting server, you can create a linked server to each production instance and run the query remotely, consolid