Smarter way to use SQLCMD with dynamic calling Smarter way to use SQLCMD with dynamic calling sql sql

Smarter way to use SQLCMD with dynamic calling


I'm only going to use this for INSERT/UPDATE or DELETE. So im not using a stored procedure. I like to use this for updating data on all our databases

If it is the case you could consider using Registered Servers:

Benefits of Registered Servers

With Registered Servers you can:

  • Register servers to preserve the connection information.

  • Determine if a registered server is running.

  • Easily connect Object Explorer and Query Editor to a registered server.

  • Edit or delete the registration information for a registered server.

  • Create groups of servers.

  • Provide user-friendly names for registered servers by providing a value in the Registered server name box that is different from the Server name list.

  • Provide detailed descriptions for registered servers.

  • Provide detailed descriptions of registered server groups.

  • Export registered server groups.

  • Import registered server groups.

  • View the SQL Server log files for online or offline instances of SQL Server

  • Execute Statements Against Multiple Servers Simultaneously

You could create multiple groups based on: server/database/environment(DEV/PROD/QA).

Another possibility is to export registered servers to file, put in source control system (SVN/Git) and share with other developers.

Using SSMS you click new query based on server group and execute the same query on multiple databases at once.

enter image description here

Image from: http://sqlmag.com/site-files/sqlmag.com/files/archive/sqlmag.com/content/content/142469/Greenwood-SQL2331-Fig5-sm.jpg


Do the following steps:

1) Create link server.

2) Run below script i have modified your code snip-it

IF OBJECT_ID('tempdb..##Results') IS NOT NULL    Truncate TABLE ##Resultselse    CREATE TABLE ##Results    (id int identity ,ServerName nvarchar(50), DbName nvarchar(50), IsDone bit default(0))      -- populat link server name and db name      insert into ##Results (ServerName,DbName) VALUES ('CZASQL-001', '[Lps_Hepper_CZ]')     insert into ##Results (ServerName,DbName) VALUES ('LS_Hepper_DK',  '[Lps_Hepper_DK]')     insert into ##Results (ServerName,DbName) VALUES ('LS_Hepper_DK',  '[Lps_Hepper_NY]')     insert into ##Results (ServerName,DbName) VALUES ('LS_Hepper_DK',  '[Lps_Hepper_Supplier]')     insert into ##Results (ServerName,DbName) VALUES ('LS_Hepper_372', '[Lps_Hepper_MO]')     insert into ##Results (ServerName,DbName) VALUES ('LS_Hepper_678', '[Lps_Hepper_678]')      WHILE (SELECT COUNT(*) FROM ##Results WHERE IsDone = 0) > 0        BEGIN        DECLARE @selectedRow INT = (SELECT TOP 1 Id FROM ##Results WHERE IsDone = 0)        DECLARE @sq1 varchar (100) ='SELECT * FROM ' ; --         DECLARE @sql varchar (8000) = '';        DECLARE @FROM varchar (100) ='[config].[LpsPlant]' ;                 select @sql = @sq1 + ServerName + '.'+ DbName+ @FROM FROM ##Results WHERE Id = @selectedRow        print @sql         exec @sql         UPDATE ##Results SET IsDone = 1 WHERE Id = @selectedRow    END;


Using a cursor, it could be:

CREATE TABLE #TEMP (ServerName nvarchar(50), DbName nvarchar(50))insert into #TEMP (ServerName,DbName) VALUES ('CZASQL-001', '[Lps_Hepper_CZ]')insert into #TEMP (ServerName,DbName) VALUES ('LS_Hepper_DK',  '[Lps_Hepper_DK]')insert into #TEMP (ServerName,DbName) VALUES ('LS_Hepper_DK',  '[Lps_Hepper_NY]')insert into #TEMP (ServerName,DbName) VALUES ('LS_Hepper_DK',  '[Lps_Hepper_Supplier]')insert into #TEMP (ServerName,DbName) VALUES ('LS_Hepper_372', '[Lps_Hepper_MO]')insert into #TEMP (ServerName,DbName) VALUES ('LS_Hepper_678', '[Lps_Hepper_678]')DECLARE @ServerName VARCHAR(100),@DatabaseName VARCHAR(100);DECLARE CRS CURSOR LOCALFOR SELECT * FROM #TEMPFETCH NEXT FROM CRS INTO @ServerName, @DatabaseName;WHILE @@FETCH_STATUS=0BEGIN    print CONVERT(NVARCHAR(100),@selectedRow)    :CONNECT @ServerName    GO    USE @DatabaseName    GO    SELECT * FROM [config].[LpsPlant]    GO    FETCH NEXT FROM CRS INTO @ServerName, @DatabaseName;ENDDROP TABLE #TEMP