List Databases in SQL Server

In SQL Server, managing and exploring databases often begins with listing the available databases on an instance. SQL Server provides multiple methods to achieve this, ranging from querying system views to using system-stored procedures or SQL Server Management Studio (SSMS).

This article explains how to list databases in SQL Server using Transact-SQL (T-SQL) commands and SSMS. It also covers best practices for managing database visibility and access permissions.


TL;DR

  • Use the sys.databases system view to list all databases:
  • Use the sp_databases stored procedure to list databases in legacy environments.
  • In SQL Server Management Studio (SSMS), view databases through Object Explorer.
  • Manage permissions to control visibility of databases for specific users.

Methods to List Databases in SQL Server

SQL Server provides multiple options to retrieve a list of databases. The choice of method depends on your environment, access level, and use case.

1. Using the sys.databases System View

The sys.databases system view provides comprehensive information about all databases in an instance of SQL Server.

Syntax:

Example Output:

List tables using the sys.databases System View
  • name: The name of the database.
  • database_id: The unique ID of the database.
  • state_desc: The current state of the database (e.g., ONLINE, RESTORING).

Use filtering to retrieve specific results.

Example: List only user-created databases:


2. Using the sp_databases Stored Procedure

The sp_databases system-stored procedure is a legacy method for listing databases. While less flexible than sys.databases, it is still useful in older environments.

Syntax:

Example Output:

List tables using the sp_databases Stored Procedure

Using SQL Server Management Studio (SSMS)

SSMS provides a graphical interface to view databases without writing SQL queries.

Steps:

  1. Open SSMS and connect to your SQL Server instance.
  2. Expand the Databases node in Object Explorer.
  3. View the list of databases, which includes both system and user databases.

Advantages:

  • Easy navigation.
  • No need to write SQL queries.

Limitations:

  • Requires SSMS installation.
  • Not suitable for automated workflows.


Managing Database Visibility

Database visibility depends on user permissions. By default, users can see all databases on an instance but can only access those for which they have permissions.

Restricting Database Visibility

Use the VIEW ANY DATABASE permission to control database visibility.

Revoke Permission:

Grant Permission:


Best Practices for Listing and Managing Databases

  1. Use System Views:
    Prefer sys.databases for detailed and customizable results.
  2. Limit Access:
    Restrict database visibility to authorized users to enhance security.
  3. Automate Listing:
    Use scripts or monitoring tools to automate database listing and health checks.
  4. Document Database Metadata:
    Maintain an updated inventory of databases, including their purpose and ownership.

Common Issues and Solutions

Permission Denied:

  • Cause: The user lacks the VIEW ANY DATABASE permission.
  • Solution: Grant the required permissions:

Hidden Databases:

  • Cause: Specific databases are hidden due to restricted permissions.
  • Solution: Check sys.databases with elevated privileges.

Legacy Environments:

  • Issue: sys.databases may not be available in older SQL Server versions.
  • Solution: Use sp_databases or upgrade to a supported version.


  1. Microsoft Documentation: sys.databases
  2. Microsoft Documentation: sp_databases
  3. SQL Server Management Studio (SSMS)
  4. SQL Server Commands for Listing Databases

Leave a Reply

Your email address will not be published. Required fields are marked *