MSSQL 跨服器调用存储过程

  • A库
  • CREATE PROCEDURE [dbo].[A_P_Test]
    AS
    BEGIN
       SELECT * FROM dbo.A_LoadData
    END
  • B库  在B中调用A库存储过程 注:是同一服务器调用。 
  • CREATE PROCEDURE [dbo].[A_P_Test2]
    AS
    BEGIN
        declare @sql nvarchar(500);
        set @sql = N' exec AMACDB_TEST.dbo.A_P_Test ';
        exec  sp_executesql @sql
    END

     

  • A,B两个数据库,不在同一台服务器实例, 在B库的存储过程中,调用A库的存储过程

      B库

     

CREATE PROCEDURE [dbo].[A_P_Test2]
AS
BEGIN
   declare @sql varchar(MAX);
   set @sql = N' exec OPENDATASOURCE(''SQLOLEDB'',''Data Source=192.168.120.13;User ID=sa;Password=sa'').AMACDB_TEST.dbo.A_P_Test ';
   exec  sp_executesql @sql
END

 

注:如果出现下面问题

消息 15281,级别 16,状态 1,第 1 行

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries'

because this component is turned off as part of the security configuration for this server.

A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure.

For more information about enabling 'Ad Hoc Distributed Queries',

see "Surface Area Configuration" in SQL Server Books Online.

解决方法如下:

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

 

posted @ 2016-04-12 20:51  DemLiu  阅读(638)  评论(0编辑  收藏  举报