跨数据库复制表数据

tableA:目标表
tableB:源表
databaseName:源数据库


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



可能会遇到的问题及解决方法

/*
导入
*/
--错误信息如下时:
--Msg 15281, Level 16, State 1, Line 2
--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.
--请选择执行下面语句,修改默认配置,在进行导入操作
-- begin
sp_configure 'show advanced options',1
RECONFIGURE WITH override
go
sp_configure 'Ad Hoc Distributed Queries',1 -- 1:启用,默认0:禁用
RECONFIGURE WITH override
go
EXEC sp_configure;
go
-- end

-- 查询
select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 5.0;HDR=YES;DATABASE=D:\ExcelSheet1.xls',sheet1$)
go
-- 查询并导入
select * into ExcelSheet from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 5.0;HDR=YES;DATABASE=D:\ExcelSheet1.xls',sheet1$)
go

/*
导出
*/
--错误信息如下时:
--Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
--SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.
--请选择执行下面语句,修改默认配置,在进行导出操作
-- begin
sp_configure 'show advanced options',1
RECONFIGURE WITH override
go
sp_configure 'xp_cmdshell',1 -- 1:启用,默认0:禁用
RECONFIGURE WITH override
go
EXEC sp_configure;
go
-- end

--添加到现在Excel文档
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 5.0;HDR=YES;DATABASE=D:\ExcelSheet.xls',sheet1$)
go
--带连接信息导出
EXEC master..xp_cmdshell 'bcp Test.dbo.ExcelSheet out "C:\ExcelSheet.xls" -c -S"LONG-LENOVOPC" -U"sa" -P"s1"'
go
--使用默认安全连接导出
EXEC master..xp_cmdshell 'bcp Test.dbo.ExcelSheet out "C:\ExcelSheet.xls" -c -T'
go
--带查询语句导出
EXEC master..xp_cmdshell 'bcp "SELECT * FROM Test.dbo.ExcelSheet" queryout "D:\ExcelSheet.xls" /c -S"LONG-LENOVOPC" -U"sa" -P"s1"'
go

/*

BCP共有四个动作可以选择。
(1) 导入 : 这个动作使用in命令完成,后面跟需要导入的文件名。

(2) 导出 : 这个动作使用out命令完成,后面跟需要导出的文件名。

(3) 使用SQL语句导出 : 这个动作使用queryout命令完成,它跟out类似,只是数据源不是表或视图名,而是SQL语句。

(4) 导出格式文件 : 这个动作使用format命令完成,后而跟格式文件名。

常用参数介绍:
-f : format_file表示格式文件名。这个选项依赖于上述的动作,如果使用的是in或out,format_file表示已经存在的格式文件,如果使用的是format则表示是要生成的格式文件。

-x : 这个选项要和-f format_file配合使用,以便生成xml格式的格式文件。

-F : first_row指定从被导出表的哪一行导出,或从被导入文件的哪一行导入。

-L : last_row指定被导出表要导到哪一行结束,或从被导入文件导数据时,导到哪一行结束。

-c : 使用char类型做为存储类型,没有前缀且以"\t"做为字段分割符,以"\n"做为行分割符。

-w : 和-c类似,只是当使用Unicode字符集拷贝数据时使用,且以nchar做为存储类型。

-t : field_term指定字符分割符,默认是"\t"。

-r : row_term指定行分割符,默认是"\n"。

-S : server_name指定要连接的SQL Server服务器的实例,如果未指定此选项,BCP连接本机的SQL Server默认实例。如果要连接某台机器上的默认实例,只需要指定机器名即可。

-U : login_id指定连接SQL Sever的用户名。

-P : password指定连接SQL Server的用户名密码。

-T : 指定BCP使用信任连接登录SQL Server。如果未指定-T,必须指定-U和-P。

-k : 指定空列使用null值插入,而不是这列的默认值。

*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值