SQLCmd Loop Through Instances on servers and execute

hello, I have to loop thru all the instances on a server, create 2 stored procedures one takes one parameter and then execute that sp to an out put file. I have it working with a batch file that looks like the code below: (the code below is in a batch file that I run from a command line. This creates the 2 stored procedures and then executes it in ONE instance). I also have a file that has the database names in it, one per line, comma delimited, but have been unable to get a loop going to use the databasenames.txt as input, loop thru, create each sp and execute the second passing in the searchstring parameter.  Please help! thank you
@ECHO OFF
SET /P srvname=Enter your machine name:
SET /P dbname=Enter your database name:
SET /P dbuser=Enter your database user name (e.g. sa):
SET /P dbpwd=Enter your  database user password:
SET /P pth=Enter the path to your scripts (include closing backslash):
SET /P searchstring=Enter the string to search for:

sqlcmd -S %srvname%  -d %dbname% -U %dbuser% -P %dbpwd%  -i"%pth%StoredProcedures\usp_GetErrorInfo.sql" >> Log_%dbname%.txt
sqlcmd -S %srvname%  -d %dbname% -U %dbuser% -P %dbpwd%  -i"%pth%StoredProcedures\usp_SearchAllObjectsForString.sql" >> Log_%dbname%.txt

sqlcmd -S %srvname%  -d %dbname% -U %dbuser% -P %dbpwd%  -Q "EXEC usp_SearchAllObjectsForString @StringToSearchFor=%searchstring%" >> Log_%dbname%.txt
myst_blackAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
myst_blackConnect With a Mentor Author Commented:
Hello, I answered my own question: here is the final batch file. It loops thru the InputServerNames.txt and for each instance name in the file, it creates the stored procedures, executes one of them with a parameter and outputs the results to a small log file for each instance:

@ECHO OFF
SET /P srvname=Enter your machine name:
SET /P dbuser=Enter your database user name (e.g. sa):
SET /P dbpwd=Enter your  database user password:
SET /P pth=Enter the path to your scripts (include closing backslash):
SET /P searchstring=Enter the string to search for:


FOR /f %%X IN (INPUTSERVERNAMES.txt) DO (
sqlcmd -S %srvname%  -d %%X -U %dbuser% -P %dbpwd%  -i"UseDatabase.sql" -v dbname=%%X >> Log_%%X.txt
sqlcmd -S %srvname%  -d %%X -U %dbuser% -P %dbpwd%  -i"%pth%StoredProcedures\usp_GetErrorInfo.sql" >> Log_%%X.txt
sqlcmd -S %srvname%  -d %%X -U %dbuser% -P %dbpwd%  -i"%pth%StoredProcedures\usp_SearchAllObjectsForString.sql" >> Log_%%X.txt
sqlcmd -S %srvname%  -d %%X -U %dbuser% -P %dbpwd%  -Q "EXEC usp_SearchAllObjectsForString @StringToSearchFor=%searchstring%" >> Log_%%X.txt

)
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Elect one instance where you create linked servers for the rest of all instances, then you can run a script for each linked server.

Other option is to install Master Data Services (only SQL Server 2012 and 2014) where you can register all the instances and run the same script over all.
0
 
myst_blackAuthor Commented:
hi thank you. The instances are on one server at this time. So I don't need to link to them, I just need to iterate thru them and create the sp's and then executed one. I can't use Master Data Services since right now these databases are all installed on SQL Server 2008 R2, BUT the compatibility level is at 80 (sql server 2000). I am looping thru all the instances and finding functions and/or stored procedures that have the old joins (*=/=*) so that we can upgrade or remove those sp's and upgrade the compatibility level and then upgrade to sql server 2014.
0
 
myst_blackAuthor Commented:
my own comment was the solution. while I waited for some assistance, I also continued to work on the script that I already had and when I got the script to work, I posted it and accepted it as the answer since none of the others comments were what I needed.
0
All Courses

From novice to tech pro — start learning today.