Method of copying data across databases in sql sever

The detailed usage has been commented in the sql code. This is the inverted data between sqlserver and sqlserver. 2005, 2008, 2012 should all apply.

copy code
--Query 100 pieces of data from the remote server 192.168.66.154, and then import them into dbo.dquestiondata 
insert  into dbo.dquestiondata
 select  top  100  *  from 
opendatasource ( ' sqloledb ' , ' data source=192.168.6.154;user id=sa; password=xxxxxx ' ).Answer.dbo.DQuestionData
 -- opendatasource is a system function, the first parameter is the Provider Name, the second parameter is the Oledb link string, 
-- note that there is no database specified in the connection string; database name , Schema, and the table name are written after the opendatasource function.

--Executing the above statement will report the following error, because remote query support is not turned on 
    -- SQL Server 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.

--Solution 1: In the graphical interface, SQL Server Security Configuration --> Functional Security Configuration --> Enable remote query Openrowset and opendatasource support

--Solution 2: Use the sp_confing system stored procedure to open it from the command line --First, 
you must open the 'show advanced options' advanced option, because 'Ad Hoc Distributed Queries' is an advanced option 
exec sp_configure ' show advanced options ' , 1 
reconfigure 
-- the following step can be omitted, sp_configure without parameters means to view all available options 
-- remember that the reconfigure statement must be added above, the purpose is to make the options take effect immediately, otherwise we 
-- still can't see 'Ad Hoc Distributed Queries' advanced options 
exec sp_configure
 -- turn on the 'Ad Hoc Distributed Queries' option 
exec sp_configure ' Ad Hoc Distributed Queries ' , 1 
reconfigure

--Remember to turn off these options
 after running out of options _ _ _ _ _ _ 
_ 
_ _ _ _


copy code

 2. Use the openrowset system function

copy code
-- 1, Microsoft OLE DB Provider for SQL Server 
-- Note that the writing of the link string in the second part is very strange. Server, user, and passwrod are connected with ";". 
select  top  100  *  from 
openrowset ( ' sqloledb ' , ' 192.168.6.154 ' ; ' sa ' ; ' xxxxx ' ,Answer.dbo.DQuestionData)

-- 2, ODBC data source method: Microsoft OLE DB Provider for ODBC Drivers 
select   top  100  *  from 
openrowset ( ' MSDASQL ' , ' DRIVER={SQL Server};SERVER=192.168.6.154;UID=sa;PWD=xxxxx ' , Answer.dbo.DQuestionData)
copy code

 The above two methods will use the name of the oledb provider. The following system stored procedure can view the name of the oledb provider

--Used to view oledb provider name 
EXEC master..xp_enum_oledb_providers

3, with a linked server

If you want to use the remote query multiple times, it is a bit troublesome to write such a long link string every time. Consider reuse.

Using a linked server can solve this problem very well

copy code
--Create linked server 
exec sp_addlinkedserver ' svr_link ' , '' , ' sqloledb ' , ' 192.168.6.154 ' 
--Create login information 
exec sp_addlinkedsrvlogin ' svr_link ' , ' false ' , null , ' sa ' , 'xxxx '

--The query format is: link server. database name. schema name. table name 
select  top  100  *  from svr_link.Answer.dbo.DQuestionData

--delete link server 
exec sp_dropserver ' svr_link ' , ' droplogins '
copy code

A few points to note:

1. Although the above examples all select data from the remote server to the local,

But we can also direct the local data to the remote server

--Insert two pieces of data into the test table of the Answer database of the remote database 192.168.6.154 
insert  into svr_link.Answer.dbo.test
 select  1 , ' zhang '  union  all 
select  2 , ' wang '

2. With the linked server, you can use the openquery system function

copy code
--Retrieve data 
select  *  from  
openquery (svr_link, ' select * from Answer.dbo.test ' )
 --Insert data 
insert  into  openquery (svr_link, ' select * from Answer.dbo.test ' )
 select  3 , ' li '
copy code

3. Both opendatasource and linked server only return the Server part. To query the table, you need to further specify the database, schema and table name. And openrowset and openquery return recordsets.

 

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

 

Method three:

Use the import and export functions that come with the Sql tool.

 

Method four:

sql server database export all data in the table into insert statement

 

    Sometimes, we want to import all the data in a certain table of the database into another database or a database on another computer, there is such a method for sql server

Below I take sql server 2008 R2, the database is Northwind database as an example,

Goal: Export the Orders table of the Northwind database into an insert statement.

 

Step 1: Select the Northwind database, right-click - Task - Generate Script:

 

Step 2: In the pop-up "Generate and Publish Script" introduction window, press the "Next" button:

 

Step 3: In the "Select Object" window, select "Select a specific database object", expand the table,

Check the table to generate the insert statement, I chose the order table here,

Press the "Next" button:

 

Step 4: In the pop-up "Set Scripting Options" window, press the "Advanced" button,

In the "Advanced Scripting Options" that pops up, drop down the drop-down bar to the bottom,

Set "Data type to be scripted" to "Data only" ("Data only" is to export only data as an insert statement, if it is to export the table structure, select "Schema Only", select "Schema and Data", then the schema and insert statements are generated), press the "OK" button:

 

Step 5: In the "Set Scripting Options" window,

"Specify how to save or publish the script" in "Output type" check "Save script to specific location",

Check "Save to file", you can specify a save path and save it as a .sql file,

Check "Save to New Query Window", a new query window will be opened, and all insert statements will be placed in the new query window:

 

Step 6: In the "Set Scripting Options" window, press the "Next" button to pop up:

Step 7: In the "Summary" window, press the "Next" button:

Step 8: In the "Save or Publish Script" window, press the "Finish" button:

Finally, a new query window will be automatically created, and all the data in the order table will be converted into an insert statement.

Guess you like

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