Method of copying table data between different databases in SQL Server

The method of copying table data between different databases:

When the table target table exists:

insert into destination database..table select * from source database..table  

When the target table does not exist:

select * into destination database..table from source database..table

--If between different SQL:  

insert into openrowset('sqloledb','destination server name';'sa';'',destination database.dbo.table) 
select * from source database..table 


--  create link server  exec  sp_addlinkedserver    ' ITSV  '  ,  ' '  ,  ' SQLOLEDB  '  ,  ' remote server name or ip address  ' exec  sp_addlinkedsrvlogin  ' ITSV  '  ,  ' false  '  ,  null  ,  ' username  '  ,  ' password  '--Query example  select * from
     
     

 
   ITSV.DatabaseName.dbo.TableName 

--  Import example  select * into  table  from  ITSV.DatabaseName.dbo.TableName  -- Remove the linked server when it is no longer used in the future  exec  sp_dropserver  ' ITSV  '  ,  ' droplogins  ' -- connect remote /LAN data (openrowset/openquery/opendatasource)  -- 1, openrowset  -- query example  select * from openrowset  (  ' SQLOLEDB  '  ,  ' sql server name  '  ;  ' user name  '  ;  ' password  '
  

 
   

 
 

 
        , database name. dbo. table name) 

--  generate  local table select * into  table  from openrowset  (  ' SQLOLEDB  '  ,  ' sql server name  '  ;  ' user name  '  ;  ' password  '  , database name. dbo. table name)  -- Import the local table into the remote table  insert openrowset  (  ' SQLOLEDB  '  ,  ' sql server name  '  ;  ' user name  '  ;  ' password  '  , database name.dbo. 表名) select*from
       

 
     
   Local table 

--  update local table  update  b  set  b. Column A  =  a. Column A  from openrowset  (  ' SQLOLEDB  '  ,  ' sql server name  '  ;  ' user name  '  ;  ' password  '  , database name.dbo.table name)  as  a  inner join  local table b  on  a.column1  =  b.column1  -- openquery usage needs to create a connection  -- first create a connection to create a linked server  exec  sp_addlinkedserver    ' ITSV  '  ,  '


      


 

 
  '  ,  '  SQLOLEDB  '  ,  '  remote server name or ip address  '  
--  query  select * FROM openquery  (ITSV,  ' SELECT * FROM database.dbo. table name  '  )  -- import the local table into the remote table  insert openquery  (ITSV,  ' SELECT * FROM database.dbo.table name  '  )  select * from  local table  -- update local table  update  b  set b.column  B  = a.column  B  FROM openquery  (ITSV,  ' SELECT * FROM database.dbo.table name 
  
  
 
  
  
 


  '  )  as  a 
inner  join  local table b  on  a. Column A  =  b. Column A 

--  3. opendatasource/openrowset  SELECT * FROM opendatasource  (  ' SQLOLEDB  '  ,  ' Data Source=ip/ServerName;User ID=login name;Password =password  '  ).test.dbo.roy_ta  -- import the local table into the remote table  insert opendatasource  (  ' SQLOLEDB  '  ,  ' Data Source=ip/ServerName;User ID=login name;Password=password  '  ).Database.dbo.table nameselect * from
     
      
 
   
  

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

好吧,如果上面看得烦下面有个更容易理解的例子:

Exec sp_droplinkedsrvlogin DBVIP,Null
Exec sp_dropserver DBVIP

EXEC sp_addlinkedserver
      @server='DBVIP',--被访问的服务器别名 
      @srvproduct='',
      @provider='SQLOLEDB',
      @datasrc='Server2'   --要访问的服务器


EXEC sp_addlinkedsrvlogin 
     'DBVIP', --被访问的服务器别名
     'false', 
     NULL, 
     'sa', --帐号
     'thankyoubobby' --密码


Select   *   from DBVIP.pubs.dbo.orders   


/////////////////////////////ORACLE////////////////////////////
Exec sp_droplinkedsrvlogin demo,Null
Exec sp_dropserver demo
go

EXEC sp_addlinkedserver 
        @server ='demo',
        @srvproduct='Oracle',
   @provider='MSDAORA', 
   @datasrc='ServiceName'

EXEC sp_addlinkedsrvlogin 
     'demo', 
     'false', 
     NULL, 
     'userid', 
     'password' 
go

SELECT * FROM OPENQUERY(demo ,'select * from tbdemo' )

UPDATE OPENQUERY (demo, 'SELECT id FROM tbdemo WHERE id = 101') 
SET name = 'hello';

INSERT OPENQUERY (demo, 'SELECT id FROM tbdemo')
VALUES ('hello');

DELETE OPENQUERY (demo, 'SELECT id FROM tbdemo WHERE name = ''hello''');

 

-------------------------------------------具体例子------------------------------------------------------------

if   exists(select   1   from   master.dbo.sysservers   where   srvname   =   'test')   
begin   
exec   sp_droplinkedsrvlogin     'test','sa'   
exec   sp_dropserver     'test'   
end  

--建立连接服务器 
EXEC sp_addlinkedserver 'test', 'ms','SQLOLEDB', '192.168.1.99'


exec sp_addlinkedsrvlogin 'test','false',null,'sa',''
select * from test.db_film.dbo.T_film
go

if   exists(select   1   from   master.dbo.sysservers   where   srvname   =   'test')   
begin   
exec   sp_droplinkedsrvlogin     'test','sa'   
exec   sp_dropserver     'test'   
end  

go

 

方法二:

1、新建一个连接服务器,连接到你要导入的服务器的IP地址(或者机器名)

2、点击安全性,使用此安全上下文建立连接,输入数据库服务器的用户名和密码

3、选择要导出的数据库,使用如下sql导数据:

select * into   laobao from [10.180.116.121].ynpdeicp.dbo.LaoBao

Sql解释:

select 要导入的字段 into   要导入的数据表 from [IP地址].数据库名.dbo.数据表名

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326010046&siteId=291194637
Recommended