Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Way to return a list of all databases on MS SQL server?

Status
Not open for further replies.

XJ900

Technical User
Apr 13, 2007
27
US
Is there a way to return a list/names of every database that is on the MS SQL server?
 
Yes.

Code:
SELECT name
FROM sysdatabases
ORDER BY name
Code:
SELECT name
FROM sys.databases
ORDER BY name

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
This will work with either. It is essentially the same as mrdenny's, but if you use this, you won't have to worry about what happens when you upgrade to sql 2005.

Code:
[COLOR=blue]If[/color] [COLOR=#FF00FF]Convert[/color]([COLOR=blue]VarChar[/color](1), ServerProperty([COLOR=red]'ProductVersion'[/color])) = [COLOR=red]'8'[/color]
	[COLOR=blue]Select[/color] [COLOR=blue]Name[/color] [COLOR=blue]from[/color] master..sysdatabases
[COLOR=blue]Else[/color]
	[COLOR=blue]Select[/color] [COLOR=blue]Name[/color] [COLOR=blue]From[/color] sys.databases

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
cheeter.

fine, be that way.

Code:
if @@microsoftversion >= 150996343
    Select Name From sys.databases
Else
    Select Name from master..sysdatabases

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Thank you, one more question:

Is there a way to do this with a DSN-less connection from a VB app?
 
DSNs aren't required to connect to a database. Just define all the SQL Server settings within the connection string and you should be able to connect.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
No problem.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top