How to implement efficient cross-Server Query on SQL Server

Source: Internet
Author: User
Tags import database

BKJIA database channel recommends "SQL Server beginners to proficient" and "database index and query topics" to help you.

Today, the product department needs to export batch data, but several tables that need to be connected for query and query are not on the same server. So I started to do this:

1. query the data of a server and import the local Excel

2. query the data of another server and import the data to a local Excel file.

3. Import Excle into the database. The database comes with the Excel import database function.

4. Connection query, OVER!

Later, I learned that the product department needed data from more than 50 cities across the country, so I had to repeat it in every city.

This reminds me of cross-database queries.

1. Activate the Distributed Query permission

 
 
  1. exec sp_configure 'show advanced options',1   
  2. reconfigure  
  3. exec sp_configure 'Ad Hoc Distributed Queries',1   
  4. reconfigure 

2. Query

 
 
  1. Select a. ProjCode as 'project No. ', B. projname as 'project name', count (1) as 'house quantity' from
  2. Openrowset ('sqlodb', 'IP address server name) '; 'username'; 'Password', [database name]. [dbo]. [Table name])
  3. , Openrowset ('sqlodb', 'IP address server name) '; 'username'; 'Password', [database name]. [dbo]. [Table name]) B
  4. Where a. ProjCode = B. newcode GROUP by a. ProjCode, B. projname order by count (1) desc

3. Then change the city. You only need to change the IP address, database name, and table name.

4. Disable

 
 
  1. exec sp_configure 'Ad Hoc Distributed Queries',0   
  2. reconfigure   
  3. exec sp_configure 'show advanced options',0   
  4. reconfigure 
 

Original article title: SQL cross-Server Query

Link: http://www.cnblogs.com/zhanglei644213943/archive/2010/05/05/1727670.html

Edit recommendations]

  1. SQL Server uses indexes to optimize data access
  2. SQL Server database optimization experience
  3. How to Use SQLServer database to query the cumulative value
  4. Analysis of Oracle and SqlServer Stored Procedure debugging and error handling
  5. SQL Server statements and stored procedures
  6. 50 methods to optimize SQL Server database queries
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.