gpt4 book ai didi

一个数据库中从另一个数据库调用的 SQL 存储过程。背景是什么?

转载 作者:行者123 更新时间:2023-12-03 02:46:46 24 4
gpt4 key购买 nike

这是一个与在另一个数据库的上下文中从一个数据库调用存储过程时的上下文相关的问题。

假设我在 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

我的问题是:当从 AuxDB.dbo.sp_action 中调用 MainDB.dbo.sp_mainproc 时,dbo.xUser搜索 sp_mainproc 中使用的表。是考虑 MainDB.dbo.xUser,还是搜索 AuxDB.dbo.xUser

谢谢, 佩特

最佳答案

Proc 是经过编译的,因此它将引用与 dbo.sp_mainproc 存在的同一数据库中的对象,因为当创建 proc 时,它仅引用 dbo.xUser ,它没有数据库名称部分

(即 MainDB.dbo.sp_mainproc 将使用 MainDB.dbo.xUser,无论从哪个数据库调用该过程)。

关于一个数据库中从另一个数据库调用的 SQL 存储过程。背景是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12215983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com