在Management Studio T-SQL查询中指定连接


9

当将用户添加为数据库服务器的角色时,我经常使用GUI中的“为此操作编写脚本”功能。然后,我只需转到“连接::更改连接”即可在其他服务器上执行相同的操作。

有没有一种方法可以在脚本操作中指定连接,因此不必执行第二个“更改连接”步骤?

Answers:


12

作为SSMS脚本的一部分,无法执行此操作,但是您有两个选择。

您可以做的一件事是使用SQLCMD模式和:: connect命令,以使脚本可以连接到多个服务器并运行该脚本。如果您为用户保存了脚本并使用:r命令从文件中加载脚本,则效果很好。

您可以做的另一件事是配置中央管理服务器,然后一次对多个服务器运行脚本。


1
“中央管理服务器”。啊,那是我目前不使用的...
gbn

是的,它是像这样的东西隐藏的宝石,比SQLCMD脚本好得多。
SQLRockstar 2011年

2

实际上,可以从T-SQL内部进行,但是您必须满足一定的条件,并且要克服一些困难。

  • 首先,您需要在运行查询的服务器上启用远程查询(OPENDATASOURCE / OPENROWSET)。
  • 其次,您需要确保目标服务器已启用远程访问。
  • 第三,您将需要大量使用动态SQL,以便可以将T-SQL代码“注入”到要执行的目标服务器的数据库引擎中。

这是一个示例脚本,使您可以利用CMS自动执行SQL任务。

/**********************************************************************/

/* Global change password script                                      */

/*                                                                    */

/* This script changes the password for a SQL login on all servers    */

/* managed by a Central Management Server. It assumes that the login  */

/* exists on all servers, and that all servers are SQL 2005 or later. */

/**********************************************************************/

DECLARE @nServer NVARCHAR (128) -- Variable to hold the instance name retrieved from the CMS

DECLARE @nSQL NVARCHAR (4000)   -- Variable to hold dynamic SQL

DECLARE @ServerFetch INT        -- Variable to hold the fetch status. In SQL 2005, the @@FETCH_STATUS

                                -- variable is scoped at the system level, so if another process is also

                                -- using a cursor the @@FETCH_STATUS variable will be set according to

                                -- that operation. This allows us to store a persistent value.


DECLARE curServer CURSOR LOCAL STATIC FOR  -- Declare the cursor with the LOCAL and STATIC options, and

                                           -- retrieve the list of server names from the Central Management

                                           -- Server. The value in the [sysmanagement_shared_server_groups_internal]

                                           -- table is user-defined; for purposes of this example we have

                                           -- created a group named "SQL2008".

    SELECT DISTINCT

    s.server_name AS 'ServerName'

    FROM OPENDATASOURCE ('SQLOLEDB', 'Data Source = CMS1\Management; Integrated Security = SSPI').msdb.dbo.sysmanagement_shared_server_groups_internal g

    INNER JOIN OPENDATASOURCE ('SQLOLEDB', 'Data Source = CMS1\Management; Integrated Security = SSPI').msdb.dbo.sysmanagement_shared_registered_servers_internal s ON g.server_group_id = s.server_group_id

    WHERE g.name = 'SQL2008'

    ORDER BY s.server_name

OPEN curServer

FETCH FIRST FROM curServer INTO @nServer       -- Retrieve the first row

SET @ServerFetch = @@FETCH_STATUS              -- Store the status of the fetch operation

WHILE @ServerFetch = 0                         -- If the fetch was successful, we enter the loop. Otherwise

                                               -- execution passes to the statement following the END statement.

    BEGIN

    -- Build the dynamic SQL to alter the password for the SQL login.

    SET @nSQL = 'EXEC OPENDATASOURCE (''SQLOLEDB'', ''Data Source = ' + @nServer

        + '; Integrated Security = SSPI'').master.dbo.sp_executesql N''ALTER LOGIN SQLLogin WITH PASSWORD = ''''<enterStrongPasswordHere>'''''

    -- Execute the dynamic SQL.

    EXEC sp_executesql @nSQL

    FETCH NEXT FROM curServer INTO @nServer    -- Retrieve the next row.

    SET @ServerFetch = @@FETCH_STATUS          -- Store the status of the fetch operation.

    END

CLOSE curServer        -- Close the cursor.

DEALLOCATE curServer   -- Remove the cursor from memory.

1

否。仅数据库按USE Database。连接不可编写脚本。

SSMS 2008(?)和其他工具提供了“在多个服务器上运行”的功能。抱歉,我当前的职位没有使用此功能,所以没有这个问题。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.