これは、別のデータベースのコンテキストであるデータベースからストアド プロシージャを呼び出すときのコンテキストに関連する質問です。
で作成されたプロシージャがあるとします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
検索されていますか?
ありがとう、ペトル