Solved

SQLCmd Loop Through Instances on servers and execute

Posted on 2014-09-23
4
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 51

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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