zoukankan      html  css  js  c++  java
  • sql server 跨数据库调用存储过程

    A库存储过程:

    create PROCEDURE [dbo].[spAAAForTest]
    (                        
        @UserName   nvarchar(20) =null ,
        @LoginPwd nvarchar(60)  =null
    )
    AS
    BEGIN
        select N'A' AS a
        , N'B' AS B
        , N'C' AS C
            ;
    
    END

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

    B库:

    ALTER PROCEDURE [dbo].[spAAAForTest2]
    (                        
        @UserName   nvarchar(20) =null ,
        @LoginPwd nvarchar(60)  =null
    )
    AS
    BEGIN
       declare @sql nvarchar(500);
       set @sql = N' exec DB_A.dbo.spAAAForTest ';
        exec  sp_executesql @sql
    
    END


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

    B库:

    ALTER PROCEDURE [dbo].[spAAAForTest2]
    (                        
        @UserName   nvarchar(20) =null ,
        @LoginPwd   nvarchar(60)  =null
    )
    AS
    BEGIN
       declare @sql nvarchar(500);
    
       set @sql = N' exec OPENDATASOURCE(''SQLOLEDB'',''Data Source=SERVER-123MSSQL2008R2;User ID=sa;Password=sa'').DB_A.dbo.spAAAForTest ';
    
       exec  sp_executesql @sql
    
    END

    --------------- 在跨服务器调用时,所使用OPENDATASOURCE 遭遇如下信息时

    消息 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

    ---------------

  • 相关阅读:
    MySQL server has gone away 问题的解决方法
    MySQL批量SQL插入性能优化
    mysql中int、bigint、smallint 和 tinyint的区别详细介绍
    Mac OS使用ll、la、l等ls的别名命令
    Github上的PHP资源汇总大全
    svn代码版本管理总结
    mysql information_schema介绍
    redis 五种数据结构详解(string,list,set,zset,hash)
    git 换行符LF与CRLF转换问题
    php 利用activeMq+stomp实现消息队列
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3557496.html
Copyright © 2011-2022 走看看