zoukankan      html  css  js  c++  java
  • SQL Server 跨库复制表数据的解决办法

    跨库复制表数据,有很多种方法,最常见的是写程序来批量导入数据了,但是这种方法并不是最优方法,今天就用到了一个很犀利的方法,可以完美在 Sql Server 2005 和 Sql Server 2008 中执行!

    格式如下:


    insert into tableA  
    SELECT * FROM          
    OPENDATASOURCE('SQLOLEDB', 'Data Source=127.0.0.1;User ID=sa;Password=sasasa').databaseName.dbo.tableB  
    

    找到这个方法后,准备执行,可是却并不太顺利,跨库复制表数据的途中,接连出现两个错误,第一个错误:

    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.

    翻译:

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。

     

    解决办法:

     

    启用  Ad Hoc Distributed Queries:

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

    待插入完成后再关闭 Ad Hoc Distributed Queries:

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

    错误2 :

    An explicit value for the identity column in table 'cms_TagSubject' can only be specified when a column list is used and IDENTITY_INSERT is ON.

    这个真的很纠结,没办法,只有 google 了,之后发现可以 在执行的 SQL 语句前后加上:

    SET IDENTITY_INSERT tableA ON
    
    --执行的SQL
    
    SET IDENTITY_INSERT tableB ON
    

    试过之后,发现这个方法并不能解决,无奈,最后 找了半天,在国外论坛找到了解决办法,就是,要写查入列的详细信息

     

    解决办法:

    insert into tableA (column1,column2.....)
    SELECT * FROM
    OPENDATASOURCE('SQLOLEDB', 'Data Source=127.0.0.1,3422;User ID=sa;Password=sasasa;').databaseName.dbo.tableB

    终于大功告成,另外,利用这种方法,还是可以直接从 Excel 里面查询的,呵呵,真强大:

    SELECT *  FROM OPENROWSET( 'MICROSOFT.JET.OLEDB.4.0','Excel 8.0;IMEX=1;HDR=YES;DATABASE=D:\a.xls',[sheet1$])

    跨库复制表数据,一种很好的方法,呵呵,希望能对大家有所帮助

  • 相关阅读:
    TDengine社区版
    进程&线程
    I2总线
    S3C2440的GPIO编程
    NPN&PNP
    旁路电容和去耦电容
    战胜C语言中令人头疼的问题
    今天神经有点大。。
    JZs3c2440裸板程序GPIO操作总结
    JZs3c2440学习笔记一
  • 原文地址:https://www.cnblogs.com/xunziji/p/2012644.html
Copyright © 2011-2022 走看看