How do I list all the databases for a given sql server 08 instance using sqlcmd?
Asked
Active
Viewed 6.5k times
4 Answers
30
EXEC sp_databases
or
SELECT NAME FROM sys.sysdatabases
or
EXEC sp_msForEachDB 'PRINT ''?''';
![](../../users/profiles/39430.webp)
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
-
5I'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_databases
2>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
![](../../users/profiles/1108891.webp)
Shaun Luttin
- 107,550
- 65
- 332
- 414
-
1Where do I find out what the server name is? Is there a command to list all the servers? – Felix Eve Aug 28 '18 at 07:48
-