I need to automate a query so I can perform it on all servers on a network. The thing is more servers are added constantly, so I need to keep it dynamic. On a table I have a list of the currently active servers, but they are repeated several times as each one has different data. Also it shows only a number, and the server name has an specific format. I did this to solve it:
select distinct ('swp0'+ cast(rtl_loc_id as nvarchar(4000)) +'r01')
from basename..inv_valid_destinations
I got this to an output file, but now I want to use it as an input to sqlcmd. Each server name (each line from previous output) should be used as an -s argument. I have tried different ways of doing this to no avail. It should be something like this:
SQLCMD -Sswp0241r01 -Uswpos -isalto_folio.sql -osalto_folio.txt
As I said, more servers appear constantly and we need to perform a query on all active servers at the time and produce an output file. Could you help me out?