Methods for accessing database objects in different database servers through an SQL statement

Source: Internet
Author: User
Tags bulk insert insert sql
Object | access | server | data | database | statement
When we do database program development, we often encounter this situation: the need to import data from one database server into a table in another database server. Usually we will use this method: first, the data in a database to be taken out of a certain, and then put the data into the destination database, this method is less efficient, writing procedures are cumbersome, error-prone. Another approach is to use a bcp or BULK INSERT statement, importing data into a file, and then exporting it to the destination database, is a little more efficient, but there are a lot of things wrong with it, and it's cumbersome to find a data import file on another machine when importing.
One of the most convenient ways, I think, is the most efficient way, which should be:
For example, there are two database servers: Zl and Ljw, there is a database taxitemp (also can be different names), the database has a table, called users, we now want to import the users data in Zl into the Ljw, You can write SQL statements like this (assuming that you are currently connected to the ZL database):
INSERT INTO Ljw.taxitemp.dbo.users
SELECT * from Users
In this way, data replication between different database servers is done through a single SQL statement.
Some people would say that I would write this SQL statement, and I thought about it, but I couldn't do it.
Indeed, such a simple statement can not be implemented, because the database does not know what the LJW server, also do not know how to log in, of course, the error.
We can solve the problem of registering a remote database server and logging on:
To register a remote database server:
EXEC sp_addlinkedserver ' ljw ', N ' SQL Server '
To register the remote database server login method:
EXEC sp_addlinkedsrvlogin ' Ljw ', ' false ', NULL, ' sa ', ' zz '
As for the detailed use of these two stored procedures, I will not say more, we look at the help to understand.

As long as we perform the remote database server registration and login method registration, we can then use the remote database as a local database.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.