29

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

Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164

4 Answers4

38
sqlcmd -E -S SERVER\INSTANCE -Q "sp_databases"
Heinzi
  • 151,145
  • 51
  • 326
  • 481
30
EXEC sp_databases

or

SELECT NAME FROM sys.sysdatabases

or

EXEC sp_msForEachDB 'PRINT ''?''';
D'Arcy Rittich
  • 153,827
  • 35
  • 271
  • 277
  • Up voted for options but accepted Heinzi because it includes the sqlcmd aspect. Thanks! – Dane O'Connor Jan 18 '10 at 17:32
  • 5
    I'm a newb and may be stating the obvious here, but I had to do 'go' after the EXEC line like this `1>EXEC sp_databases2>go` – barlop Jul 01 '15 at 20:15
29

To elaborate with more detail for the sqlcmd newbie:

C:\> sqlcmd -S <the_server_name>
1> select name from sys.databases
2> go
Shaun Luttin
  • 107,550
  • 65
  • 332
  • 414
3

You can use sp_databases stored procedure.

Giorgi
  • 28,971
  • 12
  • 82
  • 118