How to use sqlserver statement to copy a table including cross-database

How to copy a table with sqlserver statement

1, copy the table structure and data to the new table
CREATE TABLE new table SELECT * FROM the old table
This method will copy all the contents of the oldtable, of course, we can use delete from newtable; to delete.
However, one of the worst parts of this method is that the new table does not have the attributes of the primary key and Extra (auto_increment) of the old table. You need to use "alter" to add it yourself, and it's easy to make mistakes.

2. Only copy the table structure to the new table
CREATE TABLE new table SELECT * FROM old table WHERE 1=2
or CREATE TABLE new table LIKE old table

3. Copy the data of the old table to the new table (assuming the two tables have the same structure)
INSERT INTO the new table SELECT * FROM the old table

4. Copy the data of the old table to the new table (assuming that the two tables have different structures)
INSERT INTO the new table (field 1, field 2,...) SELECT field 1, field 2,... FROM the old table

5. You can copy the structure of Table 1 to Table 2
SELECT * INTO Table 2 FROM Table 1 WHERE 1=2

6. You can copy all the contents of Table 1 to Table 2
SELECT * INTO Table 2 FROM Table 1

7, show create table old table;
this will list the creation commands of the old table. We only need to copy the command and change the name of the table to create an identical table

Scene high version library is synchronized to low version library
using low version library

– Create a linked server
exec sp_addlinkedserver'ITSV','','SQLOLEDB','.\MSSQLSERVER2014'
exec sp_addlinkedsrvlogin'ITSV','false', null,'sa ', '1password'

– Query example
select top 10 * from ITSV.[Spider].[dbo].A

– Import example
select * into [SpiderWorld] from ITSV.[520musSpider].[dbo].A

Methods 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 a linked server exec sp_addlinkedserver 'ITSV', '', 'SQLOLEDB','remote server name or ip address'
exec sp_addlinkedsrvlogin 'ITSV', 'false', null,'user name','password'

– Query example
select * from ITSV. database name.dbo. table name


-Import example select * into table from ITSV. database name. dbo. table name


-Delete the linked server when it is no longer used in the future exec sp_dropserver 'ITSV', 'droplogins'

– Connect to remote/local area network data (openrowset/openquery/opendatasource)
– 1. openrowset


-Query example select * from openrowset ('SQLOLEDB','sql server name';'user name';'password', database name.dbo.table name)

- the cost of raw ground
select * into table from openrowset ( 'SQLOLEDB', ' sql server name'; 'username'; 'password', .dbo database 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 the 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 the 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 name
select * from

Note: In some cases, Ad Hoc is not turned on, use the following sentence to enable it

启用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


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',-the alias of the server being accessed
@srvproduct='',
@provider='SQLOLEDB',
@datasrc='Server2'-the server to be accessed

Sp_addlinkedsrvlogin EXEC
'DBVIP', - alias server to be accessed
'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 a connection to the 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, and enter the user name and password of the database server

3. Select the database to be exported, and use the following sql to export 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]. database name.dbo. data table name

Dakeng notes primary key auto-increment index related
1. Create a new data table with a field id, and set id as the primary key

create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )

2. Create a new data table with a field id, set id as the primary key and automatically number

create table tb(id int identity(1,1),constraint pkid primary key (id))
create table tb(id int identity(1,1) primary key )

3. A data table has been built, there is a field id in it, and the id is set as the primary key

alter table tb alter column id int not null
alter table tb add constraint pkid primary key (id)

4. Delete the primary key

Declare @Pk varChar(100);
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID(‘tb’) and xtype=‘PK’;
if @Pk is not null
exec('Alter table tb Drop '+ @Pk)

Guess you like

Origin blog.csdn.net/cao919/article/details/109577625