Solved

SQLCmd Loop Through Instances on servers and execute

Posted on 2014-09-23
4
429 Views
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
@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
0
Comment
Question by:myst_black
  • 3
4 Comments
 
LVL 45

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.
0
 

Author Comment

by:myst_black
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.
0
 

Accepted Solution

by:
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:

@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
 

Author Closing Comment

by:myst_black
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now