SQL Query To List All Databases
Chapter:
SQL Commands
Last Updated:
17-04-2023 13:41:36 UTC
Program:
/* ............... START ............... */
SELECT name FROM master.sys.databases;
/* ............... END ............... */
Output
--Sample output
name
--------------------
master
tempdb
model
msdb
AdventureWorks
AdventureWorksDW
Notes:
-
The SQL Server instance can have multiple databases, each of which may contain tables, views, stored procedures, and other database objects.
- To list all databases on a SQL Server instance, you can use the master.sys.databases system view, which contains a row for each database on the server.
- The name column of the master.sys.databases view contains the name of the database.
- To query the master.sys.databases view, you will need appropriate permissions on the SQL Server instance. Typically, the VIEW ANY DATABASE permission is required.
- Depending on your specific requirements, you may need to filter the list of databases returned by the query. For example, you could add a WHERE clause to the query to filter by a specific database name, or you could use a wildcard to search for databases that match a pattern.