Solved

SQLCmd Loop Through Instances on servers and execute

Posted on 2014-09-23
4
505 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 50

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 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