Openquery用于从链接服务器使用i / p和o / p参数执行过程

时间:2018-11-20 05:24:14

标签: sql sql-server-2008 database-administration

我需要使用openquery()从链接服务器执行一个过程

create procedure test(@ip varchar(10),@op varchar(10) output)  
as  
begin  
if @ip='a'  
begin  
set @op='Success'  
end  
end

如果假定过程与上面类似,并且我需要通过将i / p作为@ip变量传递来返回@op变量的o / p

使用此代码成功执行了SP,但是需要使用openquery()运行。

declare @op varchar(10)
execute <servername>.<dbname>.dbo.test 'a',@op OUTPUT
select @op

如何将o / p参数传递给openquery select * from openquery (<servername>,'execute <dbname>.dbo.<sp_name>') //像这样,我们可以运行没有任何参数的过程

1 个答案:

答案 0 :(得分:0)

Create Procedure [dbo].[StoredProcedureName]     
     @paramOne varchar(13),
     @paramTwo varchar(2),
     @paramThree varchar(30)

As  
   Begin Try 

          DECLARE @param_in_1 VARCHAR(13);
          DECLARE @param_in_2 VARCHAR(2);
          DECLARE @param_in_3 VARCHAR(20);
          DECLARE @param_out_1 INT = 2;
          DECLARE @param_out_2 VARCHAR(300);

          SET @param_in_1 = @paramOne;
          SET @param_in_2 = @paramTwo;
          SET @param_in_3 = @paramThree;

          EXECUTE ('BEGIN OracleStoredProcedureName(?,?,?,?,?); END;', @param_in_1, @param_in_2, @param_in_3, @param_out_1 OUTPUT, @param_out_2 OUTPUT) AT LkServerName;              

          SELECT @param_out_1, @param_out_2;

   End Try
   Begin Catch

          DECLARE @ErrorNumber INT = ERROR_NUMBER();
          DECLARE @ErrorLine INT = ERROR_LINE();
          DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
          DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
          DECLARE @ErrorState INT = ERROR_STATE();
          DECLARE @CompleteErrorMessage varchar(150);


          Print 'ErrorMessage: ' + @ErrorMessage;                
          Print 'Severity: ' + CAST(@ErrorSeverity AS VARCHAR(10));                  
          Print 'State: ' + CAST(@ErrorState AS VARCHAR(10));
          Print 'Error Number: ' + CAST(@ErrorNumber AS VARCHAR(10));
          Print 'Line: ' + CAST(@ErrorLine AS VARCHAR(10));      


   End Catch