SQL Server:如何判断数据库是否是系统数据库?
我知道到目前为止(至少在MSSQL 2005之前),系统数据库有master、model、msdb和tempdb。
事实是,据我所知,这不能保证将来会被保留。 sys.databases 视图和 sys.sysdatabases 视图都没有告诉我数据库是否被视为系统数据库。
是否有地方可以获得这些信息(数据库是否被视为系统数据库)?
I know that so far (until MSSQL 2005 at least), system databases are master, model, msdb and tempdb.
Thing is, as far as I can tell, this is not guaranteed to be preserved in the future. And neither the sys.databases view nor the sys.sysdatabases view tell me if a database is considered as a system database.
Is there someplace where this information (whether a database is considered a system database or not) can be obtained?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
刚刚深入研究
Microsoft.SqlServer.Management.Smo.Database
对象(由 Microsoft 本身提供!)他们只是使用以下语句来执行此操作:
简而言之:如果数据库名为
master
、model
、msdb
或tempdb
>,它是一个系统数据库;如果视图
sys.databases
中的字段is_distributor = 1
,它也是一个系统数据库。希望这对
吉米有帮助
Just dived into
Microsoft.SqlServer.Management.Smo.Database
object (which is provided by Microsoft itself!)They simply do this using following statement:
In short: if a database is named
master
,model
,msdb
ortempdb
, it IS a system db;it is also a system db, if field
is_distributor = 1
in the viewsys.databases
.Hope this helps
Jimmy
SQL Server Management Studio将使用此功能
如果您在“对象资源管理器”中展开“系统数据库”(从wireshark中看到),
:为了简单起见,我删除了不相关的列,删除了orderby并将@_msparam_0变量替换为其值1
SQL Server Management Studio uses this
if you expand "System Databases" in "Object Explorer" (seen from wireshark):
For the sake of simplicity I removed irrelevant columns, removed orderby and replaced @_msparam_0 variable by its value 1
您可以依靠 DB_ID() 函数 <= 4
您必须非常努力地改变这一点......
You can rely on the DB_ID() function <= 4
You'd have to work very hard to change this...
仅对于系统数据库,owner_sid 等于 0x01。
所以你可以用它来识别数据库是否是系统DB。
owner_sid is equal to 0x01 just for system databases.
So you can use it to recognise if the database is a system DB or not.
不,据我所知,没有这样的选项。我想你可以检查 sys.databases.owner_sid = 0x01 的 id。
我认为您不必担心微软更改系统数据库名称。如果他们真的这么做了,你至少 20 年就不用担心了:)
no there's no such option AFAIK. i guess you could check the id the sys.databases.owner_sid = 0x01.
i don't think you have to worry about MS changing the system db names. if they did theat you wouldn't have to worry about it for at least 20 years :)