这是在另一个数据库的上下文中从一个数据库调用存储过程时与上下文相关的问题。
说我在MainDB
创建了一个程序:
USE MainDB;
GO
CREATE PROCEDURE dbo.sp_mainproc
@Login nvarchar(50),
@Error INT OUTPUT
AS
BEGIN
-- many details left out...
-- Login as string must be captured in the xUser table to get
-- the personal settings for the user...
SET @_user_id = ( SELECT dbo.xUser.user_id
FROM dbo.xUser
WHERE dbo.xUser.login = @Login );
IF( @_user_id IS NULL )
BEGIN
-- The user with the given @Login is not present. Indicate the failure.
SET @Error = 2
RETURN (1)
END
-- Do something in the MainDB. Here the main reason for calling
-- the stored procedure is implemented.
-- Indicate the success when finishing.
SET @Error = 0
RETURN (0)
END
GO
现在,我想从AuxDB
:
USE AuxDB;
GO
CREATE PROCEDURE dbo.sp_action
AS
BEGIN
-- Call the MainDB.dbo.sp_mainproc to do the action in the MainDB.
-- The login name must be passed, and possible error must be checked.
DECLARE @error INT
DECLARE @retcode INT
EXEC @retcode = MainDB.dbo.sp_mainproc
N'the_user',
@error OUTPUT
IF (@retcode <> 0)
BEGIN
-- Here the error must be signalized.
RETURN 1
END
-- Everything OK, let's continue...
RETURN 0
END
GO
我的问题是:从MainDB.dbo.sp_mainproc
内调用AuxDB.dbo.sp_action
时,会搜索dbo.xUser
中使用的sp_mainproc
表格。考虑了MainDB.dbo.xUser
,还是搜索了AuxDB.dbo.xUser
?
谢谢, 彼得
答案 0 :(得分:5)
Procs已编译,因此它将引用dbo.sp_mainproc
所在的同一数据库中的对象,因为在创建proc时,它仅引用dbo.xUser
,而不是MainDB.dbo.sp_mainproc
数据库名称部分
(即MainDB.dbo.xUser
将使用{{1}},无论从哪个数据库调用proc。)