如何测试SQL Server数据库是否处于单用户模式

时间:2010-10-28 12:41:44

标签: sql-server

如何在SQL脚本中测试SQL Server数据库是否处于单用户模式?

2 个答案:

答案 0 :(得分:33)

您可以查看sys.databases视图...

if (SELECT user_access_desc FROM sys.databases WHERE name = 'YourDb') 
                                                            = 'SINGLE_USER'
    begin
       print 'It is in single user mode!'
    end

答案 1 :(得分:0)

SELECT CASE WHEN(SELECT 1 FROM sys.databases WHERE name ='YourDb'AND user_access_desc ='SINGLE_USER')= 1 THEN 1 ELSE 0 END AS SingleUserMode;