Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 How to execute SQL scripts in batch

Author  Topic 

BalakrishnanShanmugham
Starting Member

6 Posts

Posted - 2015-04-08 : 04:44:01
I have separate scripts for Creation of Tables, Stored Procedures, Views, Triggers in different .sql files.
I have to execute thses scripts in batch in different servers and different databases without opening SSMS.
All .sql files to be executed in single command.

Please suggest how to achieve?
Appreciate your help.

Regards,
Bala

Kristen
Test

22859 Posts

Posted - 2015-04-08 : 04:50:21
We use a BATCH file and the command line program SQLCMD e.g. something like this

SQLCMD -d MyDatabaseName -i MyScriptName.SQL -o C:\MyPath\MyScriptName.OUT -b -l 300 -S . -E >>C:\MyPath\MyScriptName.LOG
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-04-09 : 01:39:34
For a bit of extra flexibility you can invoke SQLCMD from within Powershell. see example here: http://www.sqlserver-dba.com/2009/01/powershell-sqlcmd-and-invoke-expression.html
Gives you the extra scripting options with Powershell

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
   

- Advertisement -