SQL Server Tutorial: Reading data from a Third-party database

Source: Internet
Author: User
Tags object execution functions sql ole management studio sql server management sql server management studio

You now need to access another two databases (NZ and AU) from another server (B) in the database (Master) on a single server (a). AU and NZ are part of the site's real-time data, and the current replication scheme for one step is basically consistent with master data structures from Server B to A,au/nz.

As a result, when the AU Replication to master, in the NZ Replication to master, master, only the number of NZ data left. In turn, only the AU, the specific replication is how the matter, I do not understand, also did not scrutinize.

It would be nice if you could directly operate AU and NZ in master.
SQLServer has functions:
OPENROWSET and OpenDataSource

These two functions not only enable reading of data from another database, but also read data from Excel, TXT, ORACLE, ODBC, and so on.
--Enable the ad Hoc distributed Queries:
exec sp_configure ' show advanced options ', 1
Reconfigure
exec sp_configure ' Ad Hoc distributed Queries ', 1
Reconfigure

SELECT *
From OpenDataSource (
' SQLOLE DB ',
' Data source= SQLServerName; User Id=sa; Password=xxx '
). Help.dbo.Help_Category

Or

SELECT * FROM OPENROWSET (' SQLOLE DB ', ' SQLServerName '; ' Sa '; ' xxx ', Help.dbo.Help_Category)


--Close the ad Hoc distributed Queries:
exec sp_configure ' Ad Hoc distributed Queries ', 0
Reconfigure
exec sp_configure ' show advanced options ', 0
Reconfigure


However, this writing is a bit complicated, and not easy to maintain, if the database password changed, you need to put a password to correct.


Oracle has DB Link, and the syntax is very concise.


Check the Help, SQLServer has "linked server" this dongdong. To create a linked server you need to use SQLA system stored procedure for Server:

sp_addlinkedserver [@server =] ' server ' [, [@srvproduct =] ' product_name ']
[, [@provider =] ' provider_name ']
[, [@datasrc =] ' data_source ']
[, [@location =] ' location ']
[, [@provstr =] ' provider_string ']
[, [@catalog =] ' catalog ']


exec sp_addlinkedserver @server = ' xling ', @srvproduct = ', @provider = ' SQLNcli ', @datasrc = ' SQLServerName2 '
After execution, you can see this xling in the server object-"link Server" in the Object Explorer of SQL Server Management Studio
Then you can use:
SELECT * from Xling. PricemeHelp.dbo.Help_Category

The xling here is pointing to the database SQLServerName2 's linked server, as you can see from the select above, is still complex to write.

SQLThe Server also has, and the syntax is similar:

CREATE synonym dbo. Help_category_nz for xling. PricemeHelp.dbo.Help_Category
After execution, you can use:
SELECT * from Help_category_nz
Out!

Related Article

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.