<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:表格內文; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->
-- 判斷具有指定資料庫存取權的相關資訊
USE [master]
GO
ALTER DATABASE [Test] SET OFFLINE ; -- 設置離線
GO
DECLARE @Status BIGINT
SELECT @Status= status FROM master. dbo. sysdatabases WHERE Name = 'Test'
SELECT HAS_DBACCESS ( 'Test' ) AS [ 是否可訪問 ],
DATABASEPROPERTY ( 'Test' , 'issingleuser' ) AS [ 是否單用戶 ],
@Status & ltrim ( str ( convert ( int , 0x03e0), 11)) AS [ 數據狀態為時可用 ]
/*
是否可訪問 是否單用戶 數據狀態為時可用
0 0 512
*/
GO
ALTER DATABASE [Test] SET ONLINE ; -- 設置在線
GO
DECLARE @Status BIGINT
SELECT @Status= status FROM master. dbo. sysdatabases WHERE Name = 'Test'
SELECT HAS_DBACCESS ( 'Test' ) AS [ 是否可訪問 ],
DATABASEPROPERTY ( 'Test' , 'issingleuser' ) AS [ 是否單用戶 ],
@Status & ltrim ( str ( convert ( int , 0x03e0), 11)) AS [ 數據狀態為時可用 ]
/*
是否可訪問 是否單用戶 數據狀態為時可用
1 0 0
*/
本文介绍如何使用 SQL 命令将 SQL Server 的 [Test] 数据库设置为离线和在线状态,并通过查询数据库属性来判断其当前是否可以被访问、是否处于单用户模式以及获取数据库的状态。

171万+

被折叠的 条评论
为什么被折叠?



