使用输出变量动态执行存储过程

c2e8gylq  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(389)

我有一个脚本,它使用链接的服务器引用和 OPENQUERY() 声明。为了演示,我在远程服务器上执行了以下过程:

-- ON SQL_SVR Remote Server
CREATE PROC [dbo].[MyProc]
( @Pram_A   NVARCHAR(4) OUTPUT
 ,@Pram_B   NVARCHAR(4) OUTPUT
 ,@Pram_C   NVARCHAR(4) OUTPUT
 ,@Pram_D   NVARCHAR(4) OUTPUT
)
AS
SELECT
     @Pram_A = 'foo'
    ,@Pram_B = 'bar'
    ,@Pram_C = 'this'
    ,@Pram_D = 'that'
GO

现在,我要调用此过程并检索它返回的所有四个值,因此我在本地服务器上编写了以下脚本:

-- These variables are final resting places for my data
DECLARE
     @Pram_1    NVARCHAR(4)
    ,@Pram_2    NVARCHAR(4)
    ,@Pram_3    NVARCHAR(4)
    ,@Pram_4    NVARCHAR(4);

DECLARE
     @SQL           NVARCHAR(MAX)
     -- I will pass values from the remote proc to the dynamic sql through some output parameters, which are middle men between the remote Alpha and the local Numeric params
    ,@Parms         NVARCHAR(MAX) = '@Pram_A1 NVARCHAR(4) OUTPUT, @Pram_B2 NVARCHAR(4) OUTPUT, @Pram_C3 NVARCHAR(4) OUTPUT, @Pram_D4 NVARCHAR(4) OUTPUT'
    ,@LinkedServer  VARCHAR(50) = '[SQL_SVR]';

    -- The dynamic sql expects to get the middle-men parameters, which it will fill with the values it gets for it's alpha-style params
    SET @SQL = 'SELECT * FROM OPENQUERY(' + @LinkedServer + ',''EXEC [dbo].[MyProc] @Pram_A = @Pram_A1 OUTPUT, @Pram_B = @Pram_B2,@Pram_C = @Pram_C3,@Pram_D = @Pram_D4'')';

-- Take whatever the remote proc puts into the middle-men params and put it into our local numeric params
EXEC sp_executesql @SQL, @Parms
    ,@Pram_A1 = @Pram_1 OUTPUT
    ,@Pram_B2 = @Pram_2 OUTPUT
    ,@Pram_C3 = @Pram_3 OUTPUT
    ,@Pram_D4 = @Pram_4 OUTPUT;

-- show the contents of the local, numeric params.
SELECT
     @Pram_1    AS P1
    ,@Pram_2    AS P2
    ,@Pram_3    AS P3
    ,@Pram_4    AS P4;

据我所知,这是所有正确的使用(令人钦佩的稍微复杂)参数传递,声明本地参数,将它们传递给动态sql,并在动态sql内部使用那些在远程脚本上声明的参数。但是,当我运行上述程序时,会收到以下警告和错误:

--OLE DB provider "SQLNCLI11" for linked server "PRODSQL-V2" returned message "Deferred prepare could not be completed.".

'Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@Pram_A1".'

我已经广泛地寻找一个解决方案,但我看到的一切似乎都暗示我的脚本是正确的。我知道我一定是误会了什么,犯了个错误,但我一辈子都看不到什么!你能看到我做错了什么吗?或者解释一下脚本可能失败的原因吗?
注1:使用动态sql和变量服务器名的原因在非常简化的示例脚本中没有显示。
注2:本地脚本在sql server 14.0.3048.4上,远程脚本在sql server 13.0.4001.0上

zd287kbt

zd287kbt1#

您可以在每个“作用域”中使用相同的参数名,这简化了操作,而且您必须使用 sp_executesql 没有openquery绑定输出参数。使用openquery,您必须返回resultset中的值。
比如说:

-- ON SQL_SVR Remote Server
CREATE OR ALTER PROC [dbo].[MyProc]
( @Pram_A   NVARCHAR(4) OUTPUT
 ,@Pram_B   NVARCHAR(4) OUTPUT
 ,@Pram_C   NVARCHAR(4) OUTPUT
 ,@Pram_D   NVARCHAR(4) OUTPUT
)
AS
SELECT
     @Pram_A = 'foo'
    ,@Pram_B = 'bar'
    ,@Pram_C = 'this'
    ,@Pram_D = 'that'
GO

-- These variables are final resting places for my data
DECLARE @Pram_A    NVARCHAR(4)
       ,@Pram_B    NVARCHAR(4)
       ,@Pram_C    NVARCHAR(4)
       ,@Pram_D    NVARCHAR(4);

declare @LinkedServer  VARCHAR(50) = 'SQL_SVR';
declare @SQL NVARCHAR(MAX) = concat('
  EXEC ',quotename(@LinkedServer),'.[TempDb].[dbo].[MyProc] @Pram_A = @Pram_A OUTPUT, 
                                                 @Pram_B = @Pram_B OUTPUT,
                                                 @Pram_C = @Pram_C OUTPUT,
                                                 @Pram_D = @Pram_D OUTPUT
                                    ')
declare @Parms NVARCHAR(MAX) = '@Pram_A NVARCHAR(4) OUTPUT, @Pram_B NVARCHAR(4) OUTPUT, @Pram_C NVARCHAR(4) OUTPUT, @Pram_D NVARCHAR(4) OUTPUT'

exec sp_executesql @sql, @Parms, @Pram_A = @Pram_A OUTPUT, 
                                 @Pram_B = @Pram_B OUTPUT,
                                 @Pram_C = @Pram_C OUTPUT,
                                 @Pram_D = @Pram_D OUTPUT
SELECT
     @Pram_A    AS P1
    ,@Pram_B    AS P2
    ,@Pram_C    AS P3
    ,@Pram_D    AS P4;

相关问题