ChatGPT解决这个技术问题 Extra ChatGPT

Get list of databases from SQL Server

How can I get the list of available databases on a SQL Server instance? I'm planning to make a list of them in a combo box in VB.NET.


M
Mitch Wheat

Execute:

SELECT name FROM master.sys.databases

This the preferred approach now, rather than dbo.sysdatabases, which has been deprecated for some time.

Execute this query:

SELECT name FROM master.dbo.sysdatabases

or if you prefer

EXEC sp_databases

@Gia It does exist as a backwards compatablity view. msdn.microsoft.com/en-us/library/ms179900%28v=SQL.110%29.aspx
EXEC sp_databases was slow to execute for me; 40 seconds on an instance with 36 databases. Selecting from sysdatabases was instant.
To expand on what @ChrisDiver said: SELECT name FROM sys.databases is the preferred approach now, rather than dbo.sysdatabases, which has been deprecated for a decade now.
At least on SQL Server 2014, exec sp_databases did not work. The other two (master.dbo.sysdatabases and sys.databases) still work.
G
GilShalit

in light of the ambiguity as to the number of non-user databases, you should probably add:

WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');

and add the names of the reporting services databases


V
Vishwanath Dalvi

To exclude system databases:

SELECT [name]
FROM master.dbo.sysdatabases
WHERE dbid > 6

Edited : 2:36 PM 2/5/2013

Updated with accurate database_id, It should be greater than 4, to skip listing system databases which are having database id between 1 and 4.

SELECT * 
FROM sys.databases d
WHERE d.database_id > 4

This does not work. Perhaps you meant > 4? Tables 5 & 6 are user tables.
It looks like it should always be > 4, though the server I am checking has "ReportServer" and "ReportServerTempDB" in the 5 and 6 positions.
m
marc_s
SELECT [name] 
FROM master.dbo.sysdatabases 
WHERE dbid > 4 

Works on our SQL Server 2008


System databases with ID 5 and 6 will be ReportServer and ReportServerTempDB if you have SQL Server Reporting Services installed.
B
Balaji

Don't Get confused, Use the below simple query to get all the databases,

select * from sys.databases

If u need only the User defined databases;

select * from sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb'); 

Some of the System database names are (resource,distribution,reportservice,reportservicetempdb) just insert it into the query. If u have the above db's in your machine as default.


C
Chris Diver

Since you are using .NET you can use the SQL Server Management Objects

Dim server As New Microsoft.SqlServer.Management.Smo.Server("localhost")
For Each db As Database In server.Databases
    Console.WriteLine(db.Name)
Next

var SDBLOC = new Microsoft.SqlServer.Management.Smo.Server("localhost").Databases.Cast<Microsoft.SqlServer.Management.Smo.Database>().Where(bs => !bs.IsSystemObject && bs.ID>6).ToList();
I doubt, it (localhost), is read from some configuration file. I couldn't get it worked with my "myhost" (which is my correct host name, say obtained by Environment.MachineName).Will this work if I replace "localhost" with "myhost"?
M
ManiG
SELECT [name] 
FROM master.dbo.sysdatabases 
WHERE dbid > 4 and [name] <> 'ReportServer' and [name] <> 'ReportServerTempDB'

This will work for both condition, Whether reporting is enabled or not


Careful, if your server is a named instance, the ReportServer db names are like ReportServer$InstanceName and ReportServer$InstanceNameTempDB. So this, would work either way: SELECT [name] FROM master.dbo.sysdatabases WHERE dbid > 4 and [name] NOT LIKE 'ReportServer%'
R
Rob Prouse

I use the following SQL Server Management Objects code to get a list of databases that aren't system databases and aren't snapshots.

using Microsoft.SqlServer.Management.Smo;

public static string[] GetDatabaseNames( string serverName )
{
   var server = new Server( serverName );
   return ( from Database database in server.Databases 
            where !database.IsSystemObject && !database.IsDatabaseSnapshot
            select database.Name 
          ).ToArray();
}

a oneliner: var DBsLOC = new Microsoft.SqlServer.Management.Smo.Server("localhost").Databases.Cast<Microsoft.SqlServer.Management.Smo.Database>().Where(bs => !bs.IsSystemObject && bs.ID>6).ToList(); or foreach (var Db in new Microsoft.SqlServer.Management.Smo.Server("localhost").Databases) as in .NET 4.0 + SQL Server 2014 or .SqlServer.Smo\12.0.0.0
A
Agostino

If you want to omit system databases and ReportServer tables (if installed)

select DATABASE_NAME = db_name(s_mf.database_id)
from sys.master_files s_mf
where
    s_mf.state = 0 -- ONLINE
    and has_dbaccess(db_name(s_mf.database_id)) = 1
    and db_name(s_mf.database_id) NOT IN ('master', 'tempdb', 'model', 'msdb')
    and db_name(s_mf.database_id) not like 'ReportServer%'
group by s_mf.database_id
order by 1;

This works on SQL Server 2008/2012/2014. Most of query comes from "sp_databases" system stored procedure. I only removed unneeded column and added where conditions.


While some of the others SQL code works well, this makes sure the DB is also ONLINE, which is one of the criteria that I was looking for.
w
watch_amajigger

Not sure if this will omit the Report server databases since I am not running one, but from what I have seen, I can omit system user owned databases with this SQL:

    SELECT  db.[name] as dbname 
    FROM [master].[sys].[databases] db
    LEFT OUTER JOIN  [master].[sys].[sysusers] su on su.sid = db.owner_sid
    WHERE su.sid is null
    order by db.[name]

J
JerryOL

In SQL Server 7, dbid 1 thru 4 are the system dbs.


t
thedanotto

perhaps I'm a dodo!

show databases; worked for me.


Not in SQL Server
this command worked for me as well, other result even the one with 528 votes did not work.
Are you sure you're using MSSQL? Not MySQL?
I have no idea what I'm doing, but some have found this helpful
g
gobi

If you are looking for a command to list databases in MYSQL, then just use the below command. After login to sql server,

show databases;


This doesn't add anything to other answers (plus, for sql-server is not correct) stackoverflow.com/a/41904965/2308683
L
Luca

To exclude system databases :

SELECT name FROM master.dbo.sysdatabases where sid <>0x01

This excluded most of my database.
Why are you adding the where clause? This will exclude the system databases, the OP is not asking for this. And next time if you add an answer explain what the query does. And next to that there is no sid its column on the sys.databases table its owner_sid