在sp_executesql中执行远程存储过程

Ale*_*lex 2 sql-server stored-procedures linked-server remote-server

我正在尝试在链接服务器上获取IDENT_CURRENT值。我已经在具有输出参数的远程服务器上创建了一个存储过程sp_current_identity。

CREATE  PROCEDURE [dbo].[sp_current_identity] ( @strTableName nvarchar(255), @intRowId int OUTPUT )
AS
BEGIN
select IDENT_CURRENT(@strTableName)
END
Run Code Online (Sandbox Code Playgroud)

之后,我创建了两个同义词:sp_current_identity和sometable。

我需要使用sp_executesql执行sp_current_identity(我正在创建一个自定义DataAtapter,以通过LLBLGEN 3.1使用同义词)。请参见以下示例:

declare @p4 int
set @p4=NULL
exec sp_executesql N'SET XACT_ABORT ON; INSERT INTO [db].[dbo].[sometable] ([FieldName], [TableName], [UserField]) VALUES (@p1, @p3, @p4) ;
exec dbo.sp_current_identity @p5, @p2 
;SET XACT_ABORT OFF',N'@p1 varchar(50),@p2 int output,@p3 varchar(50),@p4 varchar(50), @p5 varchar(200)',
@p1='test24',@p2=@p4 output,@p3='test24',@p4='test5',@p5='sometable'
select @p4
Run Code Online (Sandbox Code Playgroud)

在远程服务器上执行此代码(sp_current_identity是本地存储过程)时,它可以正常工作,但是在本地服务器上执行该代码时,它将导致异常。这是错误:

过程或函数“ sp_current_identity”需要未提供的参数“ @strTableName”。

谢谢你的帮助!

Aar*_*and 5

您是否考虑过运行EXEC remoteserver.database.dbo.sp_executesql 'dynamic SQL';而不是尝试在本地执行动态SQL?sp_current_identity过程必须存在于实际执行查询的位置,而不是从中调用查询的位置。

  • 但这仅仅是因为您使用同义词吗?这就像在说我不能买一辆无铅汽油车是因为我有一罐不想浪费的柴油吗? (2认同)