判斷指定資料庫存取權

本文介绍如何使用 SQL 命令将 SQL Server 的 [Test] 数据库设置为离线和在线状态,并通过查询数据库属性来判断其当前是否可以被访问、是否处于单用户模式以及获取数据库的状态。

<!-- [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

*/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值