LOGIN,並登入本機伺服器。locallogin 為 sysname,預設值為 NULL。NULL 表示這個項目適用於所有連線至 rmtsrvname 的本機登入。如果不是 NULL,locallogin 則為 SQL Server 登入或 Windows 登入。如果您使用 Windows 登入,則必須直接存取 SQL Server,或透過具有存取權的 Windows 群組成員資格存取。
LOGIN,並登入本機伺服器。locallogin 為 sysname,預設值為 NULL。NULL 表示這個項目適用於所有連線至 rmtsrvname 的本機登入。如果不是 NULL,locallogin 則為 SQL Server 登入或 Windows 登入。如果您使用 Windows 登入,則必須直接存取 SQL Server,或透過具有存取權的 Windows 群組成員資格存取。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Manage linked servers\n\n\u003cbr /\u003e\n\nMySQL \\| PostgreSQL \\| SQL Server\n\n\u003cbr /\u003e\n\nThis page provides information about how to manage linked servers on your\nCloud SQL instance, including enrolling an instance, adding a linked\nserver, and querying a linked server.\n\nEnroll your instance to allow linked servers\n--------------------------------------------\n\nTo add the `cloudsql enable linked servers` flag to your instance, use the\nfollowing command: \n\n```bash\ngcloud sql instances patch INSTANCE_NAME --database-flags=\"cloudsql enable linked servers\"=on\n```\n\nReplace \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e with the name of the instance\nthat you want to use for linking servers.\n\nFor more information, see [configure database\nflags](https://cloud.google.com/sql/docs/sqlserver/flags#config).\n\nAdd a linked server\n-------------------\n\nTo add a linked server, run the Transact-SQL `sp_addlinkedserver` command: \n\n```bash\nEXEC master.dbo.sp_addlinkedserver\n @server = N'\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @srvproduct=N'',\n @provider=N'SQLNCLI',\n @datasrc=N'\u003cvar translate=\"no\"\u003eTARGET_SERVER_ID\u003c/var\u003e'\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server to create.\n- \u003cvar translate=\"no\"\u003eTARGET_SERVER_ID\u003c/var\u003e with the network name, DNS name, or IP address for the linked server. For the instance name, use the format `servername\\instancename`. If your instance uses a non-standard port, add the port number. For example, `servername\\instancename, 8080`.\n\nTo add user mapping for a user who is currently logged in, run the following\ncommand: \n\n```bash\nEXEC master.dbo.sp_addlinkedsrvlogin\n @rmtsrvname=N'\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @useself=N'True'\n```\n\nReplace \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server.\n\nTo create or update the default remote login and password, and apply it to all\nlocal logins, run the following command: \n\n```bash\nEXEC master.dbo.sp_addlinkedsrvlogin\n @rmtsrvname=N'\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @useself=N'False',\n @locallogin=N'\u003cvar translate=\"no\"\u003eLOGIN\u003c/var\u003e',\n @rmtuser=N'\u003cvar translate=\"no\"\u003eUSER_ID\u003c/var\u003e',\n @rmtpassword='\u003cvar translate=\"no\"\u003ePASSWORD\u003c/var\u003e';\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server.\n- \u003cvar translate=\"no\"\u003eLOGIN\u003c/var\u003e with the login for the local server. `locallogin` is sysname, with a default of NULL. NULL specifies that this entry applies to all local logins that connect to `rmtsrvname`. If not NULL, `locallogin` is either a SQL Server login or a Windows login. If you use a Windows login, it must have access to the SQL Server either directly, or through its membership in a Windows group that has access.\n- \u003cvar translate=\"no\"\u003eUSER_ID\u003c/var\u003e with the user logging in.\n- \u003cvar translate=\"no\"\u003ePASSWORD\u003c/var\u003e with the user password.\n\nAdd a linked server with an encrypted connection\n------------------------------------------------\n\nTo add a linked server using an encrypted connection, run the Transact-SQL\n`sp_addlinkedserver` command: \n\n```bash\nEXEC master.dbo.sp_addlinkedserver\n @server = N'\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @srvproduct=N'',\n @provider=N'SQLNCLI',\n @datasrc=N'\u003cvar translate=\"no\"\u003eTARGET_SERVER_ID\u003c/var\u003e,\n @provstr=N'Encrypt=yes;'\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server to create.\n- \u003cvar translate=\"no\"\u003eTARGET_SERVER_ID\u003c/var\u003e with the name of the target server, or the IP address and port number for the target server.\n\nIf the name of the server is different from the name in the certificate, you\nneed to indicate that the SQL Server can trust the server certificate. To update\nthe provider string, run the following command: \n\n```bash\nEXEC master.dbo.sp_addlinkedserver\n @server = N'\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @srvproduct=N'',\n @provider=N'SQLNCLI',\n @datasrc=N'\u003cvar translate=\"no\"\u003eTARGET_SERVER_ID\u003c/var\u003e,\n @provstr=N'Encrypt=yes;TrustServerCertificate=yes;'\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server to create.\n- \u003cvar translate=\"no\"\u003eTARGET_SERVER_ID\u003c/var\u003e with the name of the target server, or the IP address and port number for the target server.\n\nLinked server queries\n---------------------\n\nCloud SQL supports the use of four-part names to query linked servers (server\nname, database name, schema name, and object name), in addition to the\nfollowing commands:\n\n- [OPENQUERY](https://learn.microsoft.com/en-us/sql/t-sql/functions/openquery-transact-sql?view=sql-server-ver16) executes a query on a specified server.\n- [EXECUTE](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/execute-transact-sql?view=sql-server-ver16) allows you to run dynamic SQL against a linked server.\n\n| **Note:** Cloud SQL doesn't support OPENROWSET as a way to query a linked server.\n\nFor more information, see [Compare query remote execution\noptions](https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/linked-servers-openquery-openrowset-exec-at?view=sql-server-ver16).\n\nEnable remote procedure calls on a linked server\n------------------------------------------------\n\nRemote procedure calls (RPC) let you run stored procedures on linked servers. To\nadd RPC, you run the Transact-SQL\n[`sp_serveroption`](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-serveroption-transact-sql?view=sql-server-ver16)\ncommand with an RPC argument. There are two RPC arguments:\n\n- `rpc` enables RPC from the specified server.\n- `rpc out` enables RPC to the specified server.\n\nUse the following `sp_serveroption` command with the `rpc` argument to enable\nRPC from \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e: \n\n```bash\nEXEC sp_serveroption\n @server='\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @optname='rpc',\n @optvalue='TRUE'\n```\n\nReplace \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server.\n\nUse the following `sp_serveroption` command with the `rpc out` argument to\nenable RPC: \n\n```bash\nEXEC sp_serveroption\n @server='\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @optname='rpc out',\n @optvalue='TRUE'\n```\n\nReplace \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server.\n\nRemove the user mapping for a linked server\n-------------------------------------------\n\nTo remove a user mapping that you previously added, run the following\ncommand: \n\n```bash\nEXEC master.dbo.sp_droplinkedsrvlogin\n @rmtsrvname=N'\u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e',\n @locallogin=N'\u003cvar translate=\"no\"\u003eLOGIN\u003c/var\u003e';\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eLINKED_SERVER_NAME\u003c/var\u003e with the name of the linked server to drop.\n- \u003cvar translate=\"no\"\u003eLOGIN\u003c/var\u003e with the login for the local server. `locallogin` is sysname, with a default of NULL. NULL specifies that this entry applies to all local logins that connect to `rmtsrvname`. If not NULL, `locallogin` is either a SQL Server login or a Windows login. If you use a Windows login, it must have access to the SQL Server either directly, or through its membership in a Windows group that has access.\n\nRemove an enrolled instance from allowing linked servers\n--------------------------------------------------------\n\nTo remove linked servers, do the following:\n\n- Drop existing linked servers with the Transact-SQL [sp_dropserver](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-dropserver-transact-sql?view=sql-server-ver16) stored procedure.\n- Remove the `cloudsql enable linked servers` flag to remove an enrolled instance.\n\n ```bash\n gcloud sql instances patch INSTANCE_NAME --database-flags=\"cloudsql enable linked servers\"=off\n ```\n\n Replace \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e with the name of the instance\n where you want to remove linked servers.\n\n Alternatively, you can run the following command to clear all database flags: \n\n ```bash\n gcloud sql instances patch INSTANCE_NAME --clear-database-flags\n ```\n\n Replace \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e with the name of the instance\n where you want to remove linked servers.\n\nTroubleshoot\n------------\n\nWhat's next\n-----------\n\n- Learn about [using linked servers](/sql/docs/sqlserver/linked-servers)."]]