开发者

How can I get all the database names in a sql server instance using tsql?

How can I get all the database names in a 开发者_如何学Pythonsql server instance using tsql?


SELECT * FROM sys.databases


----SQL SERVER 2005 System Procedures

EXEC sp_databases
EXEC sp_helpdb

----SQL 2000 Method still works in SQL Server 2005

SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases

----SQL SERVER Un-Documented Procedure

EXEC sp_msForEachDB 'PRINT ''?'''

to know more about database : http://blog.sqlauthority.com/2007/05/12/sql-server-2005-list-all-the-database/


this should work on pretty much any version of sql server

USE master;
SELECT NAME FROM sysdatabases;

[edit : it could be SELECT NAME FROM sys.databases too, microsoft's website says both and i'm not on my windows box to test, sorry!]

you could also use (sql 2005 only)

USE master;
EXEC sp_databases;


And for practical use added couple of common filters:

  select    database_id, [name] database_name                    
    from  master.sys.databases
    WHERE state <> 6                            -- skip offline
      AND database_id > 4                       -- skip system dbs
      AND HAS_DBACCESS([name]) = 1              -- with User Access


SQL 2000

use master
select name from sysdatabases

or (no need for use master, includes database_size)

exec sp_databases
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜