SQL Server 查询所有可用的数据库

查询方式1

SELECT Name FROM Master..SysDatabases ORDER BY Name

查询方式2(过滤了master等系统数据库))

SELECT dtb.name AS [Database_Name] FROM master.sys.databases AS dtb 

WHERE (CAST(case when dtb.name in ('master','model','msdb','tempdb') then 1 else dtb.is_distributor end AS bit)=0 

        and CAST(isnull(dtb.source_database_id, 0) AS bit)=0) 

ORDER BY [Database_Name] ASC

查询方式3

SELECT  dtb.name AS [Database_Name] ,

        'Server[@Name='

        + QUOTENAME(CAST(SERVERPROPERTY(N'Servername') AS SYSNAME), '''')

        + ']' + '/Database[@Name=' + QUOTENAME(dtb.name, '''') + ']' AS [Database_Urn] ,

        CASE WHEN DATABASEPROPERTY(dtb.name, 'IsShutDown') IS NULL THEN 0x200

             ELSE 0

        END | CASE WHEN 1 = dtb.is_in_standby THEN 0x40

                   ELSE 0

              END | CASE WHEN 1 = dtb.is_cleanly_shutdown THEN 0x80

                         ELSE 0

                    END | CASE dtb.state

                            WHEN 1 THEN 0x2

                            WHEN 2 THEN 0x8

                            WHEN 3 THEN 0x4

                            WHEN 4 THEN 0x10

                            WHEN 5 THEN 0x100

                            WHEN 6 THEN 0x20

                            ELSE 1

                          END AS [Database_Status] ,

        dtb.compatibility_level AS [Database_CompatibilityLevel] ,

        dmi.mirroring_role AS [Database_MirroringRole] ,

        COALESCE(dmi.mirroring_state + 1, 0) AS [Database_MirroringStatus] ,

        dtb.recovery_model AS [RecoveryModel] ,

        dtb.user_access AS [UserAccess] ,

        dtb.is_read_only AS [ReadOnly] ,

        dtb.name AS [Database_DatabaseName2],

        dtb.create_date

        

FROM    master.sys.databases AS dtb

        LEFT OUTER JOIN sys.database_mirroring AS dmi ON dmi.database_id = dtb.database_id

WHERE   ( CAST(CASE WHEN dtb.name IN ( 'master', 'model', 'msdb', 'tempdb' )

                    THEN 1

                    ELSE dtb.is_distributor

               END AS BIT) = 0

          AND CAST(ISNULL(dtb.source_database_id, 0) AS BIT) = 0

        )

ORDER BY [Database_Name] ASC

go

USE [master]

go

查询方式4

SELECT  dtb.name AS [Database_Name] ,

        CASE WHEN DATABASEPROPERTY(dtb.name, 'IsShutDown') IS NULL THEN 0x200

             ELSE 0

        END | CASE WHEN 1 = dtb.is_in_standby THEN 0x40

                   ELSE 0

              END | CASE WHEN 1 = dtb.is_cleanly_shutdown THEN 0x80

                         ELSE 0

                    END | CASE dtb.state

                            WHEN 1 THEN 0x2

                            WHEN 2 THEN 0x8

                            WHEN 3 THEN 0x4

                            WHEN 4 THEN 0x10

                            WHEN 5 THEN 0x100

                            WHEN 6 THEN 0x20

                            ELSE 1

                          END AS [Database_Status] ,

        dtb.compatibility_level AS [Database_CompatibilityLevel] ,

        dtb.user_access AS [UserAccess] ,

        dtb.is_read_only AS [ReadOnly] ,

        dtb.create_date

FROM    master.sys.databases AS dtb

        LEFT OUTER JOIN sys.database_mirroring AS dmi ON dmi.database_id = dtb.database_id

WHERE   ( CAST(CASE WHEN dtb.name IN ( 'master', 'model', 'msdb', 'tempdb' )

                    THEN 1

                    ELSE dtb.is_distributor

               END AS BIT) = 0

          AND CAST(ISNULL(dtb.source_database_id, 0) AS BIT) = 0

        )

ORDER BY [Database_Name] ASC

go

USE [master]

go
—— 完 ——
相关推荐
评论

立 为 非 似

中 谁 昨 此

宵 风 夜 星

。 露 , 辰

文章点击榜

细 无 轻 自

如 边 似 在

愁 丝 梦 飞

。 雨 , 花