SQLSERVER复制数据库某一个表到另一个数据库中(可跨服务器)

时间:2024-03-05 22:04:39
一、复制数据库某一个表到另一个数据库中(同一服务器)
SELECT * INTO 表1 FROM 表2 --复制表2如果只复制结构而不复制内容或只复制某一列只要加WHERE条件就好了
例子:SELECT * INTO [IMCDB].[dbo].[SysLog] FROM [AimManageDB].[dbo].[SysLog] 
(将数据库AimManageDB中的SysLog表复制到数据库IMCDB中)

二、复制数据库某一个表到另一个数据库中(跨服务器) 
例子:select * INTO [SMSDB].[dbo].[SysLog] FROM openrowset(\'sqloledb\',‘目标服务器’;\'账号\';\'密码\',[SMSDB].[dbo].[SysLog])
(将数据库目标服务器中的SysLog表复制本地的数据库SMSDB中)


eg:如果出现以下错误:
SQL Server 阻止了对组件 \'Ad Hoc Distributed Queries\' 的 STATEMENT\'OpenRowset/OpenDatasource\' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

解决方法:

1、系统管理员可以在本地SQL中通过使用 sp_configure 启用 \'Ad Hoc Distributed Queries\'
exec sp_configure \'show advanced options\',1 reconfigure exec sp_configure \'Ad Hoc Distributed Queries\',1 reconfigure
2、使用完成后,关闭Ad Hoc Distributed Queries:
exec sp_configure \'Ad Hoc Distributed Queries\',0 reconfigure exec sp_configure \'show advanced options\',0 reconfigure 其他本地导入方法 select * from table1 into table2 table2必须不存在 insert into table2 select * from table1 table2必须存在