Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Three Way To List All Databases On Your Server

From Wiki

Jump to: navigation, search

Sometimes you want to print out a document with all the databases that exists on one server so that you can compare this against another server for example Listed below are three ways to accomplish this by using a system table, a system view and an undocumented stored procedure

First Way: Use the INFORMATION_SCHEMA.SCHEMATA system view

  1. SELECT CATALOG_NAME FROM INFORMATION_SCHEMA.SCHEMATA
  2. WHERE CATALOG_NAME NOT IN('master','msdb','tempdb','model')
  3. ORDER BY CATALOG_NAME


Second Way: Use the sysdatabases system table in the master database

SQL Server 2000/2005

  1. SELECT name FROM master..sysdatabases
  2. WHERE name NOT IN('master','msdb','tempdb','model')
  3. ORDER BY Name


SQL Server 2005 only

  1. SELECT name
  2. FROM sys.sysdatabases
  3. WHERE name NOT IN('master','msdb','tempdb','model')
  4. ORDER BY Name


Third way: Use the undocumented sp_MSForEachDB procedure

  1. CREATE TABLE #AllDB (Name VARCHAR(100))
  2. INSERT INTO #AllDB
  3. EXEC sp_MSForEachDB 'IF ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
  4. SELECT ''?'' '
  5. SELECT * FROM #AllDB



Contributed by: --SQLDenis 15:54, 6 June 2008 (GMT)


Part of SQL Server Admin Hacks

340 Rating: 2.8/5 (5 votes cast)