跨Server, 執行SQL

  • 3732
  • 0
  • SQL
  • 2016-02-15

跨Server, 執行SQL

假設目前在Server A(140.125.243.59), 執行"新增查詢",左下角可以知道目前Server IP


然後依序執行以下SQL語法 :

 

--連結Server B(140.125.243.88)

EXEC sp_addlinkedserver '140.125.243.88', N'SQL Server'

 

--登入Server B (IP, 'false', NULL, User_ID, Password)

EXEC sp_addlinkedsrvlogin '140.125.243.88', 'false', NULL, '帳號', '密碼'

 

--查看可以使用的Server

EXEC sp_helpserver

 

--在Server B, 做Select

Select *

  From [140.125.243.88].[OpenInfo_Test_DB2].[dbo].[SYS_RPT_STUDENT6]

   Where year = '103'

   And Semester = '1'

   And University_Type = 'xxxx'

 

--跨Server做insert into

insert into [140.125.243.88].[OpenInfo_Test_DB2].[dbo].[SYS_RPT_STUDENT6]

      (

       [YEAR]

       )

Select *     

  From [140.125.243.59].[OpenInfo_Test_DB2].[dbo].[SYS_RPT_STUDENT6]

   Where year = '103'

   And Semester = '1'

   And University_Type = 'XXXX'

 

--登出Server

EXEC sp_droplinkedsrvlogin @rmtsrvname = '140.125.243.88', @locallogin = NULL

 

--中斷連結

EXEC sp_dropserver @server = '140.125.243.88'