SQLCmd Loop Through Instances on servers and execute

Posted on 2014-09-23
Last Modified: 2014-09-28
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
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
Question by:myst_black
  • 3
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40338944
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.

Author Comment

ID: 40339048
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.

Accepted Solution

myst_black earned 0 total points
ID: 40339328
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:

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:

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


Author Closing Comment

ID: 40348389
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.

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 48
Help with SQL Server Polygon 2 32
Can > be used for a Text field 6 37
SQL - Use results of SELECT DISTINCT in a JOIN 4 14
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question