为什么 MSSQL 中的一些存储过程必须在前面加上 `master..` 才能调用,而其他的则可以不加?

Why some stored procedures in MSSQL must be called with `master..` prepended while other can be called without?

有问题的 MS SQL 服务器版本是 8。

在 master 以外的数据库上下文中,如果我从 master 数据库调用存储过程,对于其中一些我必须附加 master.. 前缀(否则我会得到 Could not find stored procedure 'procname' 错误),对于其中一些我没有。 例如,我可以调用 --

EXEC sp_addlogin 'user' 'pass';

-- 并且有效,但是--

EXEC xp_cmdshell 'command'; 

-- 没有。我必须在前面添加 master.. 才能正常工作 --

EXEC master..xp_cmdshell 'command';

我在这里可能是错的,但我观察到必须将 master.. 添加到仅以 xp_ 开头的存储过程(而不是 sp_)。

为什么我必须调用其中一些带有 master.. 前缀而其中一些可以不带前缀调用?

master 数据库中名称以sp_ 开头的过程可以在任何其他用户数据库中调用,而无需添加master.. 前缀。由于以 xp_ 开头的程序不遵循该规则,因此在调用它们时仍需要添加 master.. 前缀。

See this link for more information.

xp_代表扩展存储过程。它们存储在 master 数据库中。

sp_ 用于 "special"。它们是 Microsoft 提供的过程,存在于每个数据库中。您可以通过转到(数据库名称)> 可编程性> 存储过程> 系统存储过程在对象资源管理器中查看它们。因为它们与您的查询位于同一数据库中,所以您不需要添加 master..

作为旁注,最好不要使用 sp_... 命名您自己的存储过程。请参阅说明 here