SQL statement for different databases on different servers and local servers

Source: Internet
Author: User

You may have many SQL server operations on the same server. However, how can I write SQL statements that require cross-database query or data import.

Example:

Insert   Into Database Name. DBO. Table Name Select   *   From Database Name 2. DBO. Table Name -- The structure of the queried table must be consistent with that of the preceding table.

What should I do if this is a change in requirements, not on the same server. But on different servers, what should we do?

CodeAs follows:

-- Create a connection Server
Exec Sp_addmediaserver ' Alias ' , '' , ' Msdasql ' , Null , Null , ' Driver = {SQL Server}; server = server name; uid = user name; Pwd = password; '

-- Query example
Select   *   From Alias. Database Name. DBO. Table Name
Insert Database Name. DBO. Table Name Select   *   From Alias.. Database Name. DBO. Table Name
Select   *   Into Database Name. DBO. New table name From Alias.. Database Name. DBO. Table Name

-- query the Connected Server
select * from sys. servers

If you do not need to use the linked server
--Delete A linked server
ExecSp_dropserver'Alias','Droplogins'  If the SA logon fails, use a link.Exec sp_addmediaserver 'srv _ lnk ', '', 'sqlodb', 'remote server name or IP address'
Exec sp_add1_srvlogin 'srv _ lnk ', 'false', null, 'username', 'Password'
Go

 

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.