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