You are here:Home » SQL Server » Run Script at once in all the databases of single instance of SQL Server

Run Script at once in all the databases of single instance of SQL Server

Created IO.BAT file with defining server, database, user name and password. Then Put your task batch or Stored procedures in IN.SQL file which you want to run in all the databases defined in IO.BAT. After that double click or run IO.bat file, which will execute scripts from IN.SQL and generate output for each run for every databases for any errors occurred or not.
IO.BAT 
-------Content--------------------------
OSQL -UUsername -PPassword -SServerName -dDatabasename1 -i C:\OSQL\IN.SQL -o C:\OSQL\DatabaseName1_Out.TXT
OSQL -UUsername -PPassword -SServerName -dDatabasename2 -i C:\OSQL\IN.SQL -o C:\OSQL\DatabaseName2_Out.TXT
OSQL -UUsername -PPassword -SServerName -dDatabasename3 -i C:\OSQL\IN.SQL -o C:\OSQL\DatabaseName3_Out.TXT
OSQL -UUsername -PPassword -SServerName -dDatabasename4 -i C:\OSQL\IN.SQL -o C:\OSQL\DatabaseName4_Out.TXT
OSQL -UUsername -PPassword -SServerName -dDatabasename5 -i C:\OSQL\IN.SQL -o C:\OSQL\DatabaseName5_Out.TXT
----------------------------------------

IN.SQL
-------Content--------------------------
Your tsql batch or stored procedures
----------------------------------------

DatabseNames_*.TXT
-------Content--------------------------
Output generated of IN.SQL for databases 
generated individual file for each
--------------------------------------------