Dane O'Connor
Dane O'Connor

Reputation: 77368

SqlServer 08: Query to list all databases in an instance?

How do I list all the databases for a given sql server 08 instance using sqlcmd?

Upvotes: 38

Views: 88298

Answers (4)

Heinzi
Heinzi

Reputation: 172448

sqlcmd -E -S SERVER\INSTANCE -Q "sp_databases"

Notes:

  • -E: Use a trusted connection ("Windows authentication"). Replace by -U username -P password for SQL Server authentication.
  • -S SERVER\INSTANCE: The instance of SQL Server to which to connect. If you don't know the name of your instance, you can use sqlcmd -L to get a list.
  • -Q: The query to execute. The uppercase Q causes sqlcmd to exit after executing the query.

Upvotes: 54

Shaun Luttin
Shaun Luttin

Reputation: 141672

To elaborate with more detail for the sqlcmd newbie:

C:\> sqlcmd -S <the_server_name>
1> select name from sys.databases
2> go

Upvotes: 39

Giorgi
Giorgi

Reputation: 30883

You can use sp_databases stored procedure.

Upvotes: 3

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171559

EXEC sp_databases

or

SELECT NAME FROM sys.sysdatabases

or

EXEC sp_msForEachDB 'PRINT ''?''';

Upvotes: 34

Related Questions