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. table name) 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 _
-----------------------------------------------------------------------------------------------------------------------------------
Well, if the above is annoying, here is an easier-to-understand example:
Exec sp_droplinkedsrvlogin DBVIP, Null
Exec sp_dropserver DBVIP
EXEC sp_addlinkedserver
@server='DBVIP',--server alias to be accessed
@srvproduct='',
@provider='SQLOLEDB',
@datasrc='Server2'--server to be accessed
EXEC sp_addlinkedsrvlogin
'DBVIP', --accessed server alias
'false',
NULL,
'sa', --account '
thankyoubobby' --password
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''');
-------------------------------------------Specific example----- -------------------------------------------------- -----
if exists(select 1 from master.dbo.sysservers where srvname = 'test')
begin
exec sp_droplinkedsrvlogin 'test','sa'
exec sp_dropserver 'test'
end
--establish connection server
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
Method Two:
1. Create a new connection server and connect to the IP address (or machine name) of the server you want to import.
2. Click Security, use this security context to establish a connection, enter the username and password of the database server
3. Select the database to be exported, and use the following sql to import the data:
select * into laobao from [10.180.116.121].ynpdeicp.dbo.LaoBao
Sql explanation:
select the field to be imported into the data table to be imported from [IP address].DatabaseName.dbo.DataTableName