Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#

Run Scripts Add-In for SQL Server 2005 Management Studio

Rate me:
Please Sign up or sign in to vote.
2.78/5 (8 votes)
20 Nov 2008CPOL2 min read 61.1K   727   26   7
An add-in for SQL Server 2005 Management Studio that will run all the scripts in the selected project.

Sample Image - SSMSScriptRunner.jpg

Introduction

The new SQL Server 2005 Management Studio is a great improvement over older SQL Server tools, and even allows one to create a SQL Script project type to organize your SQL scripts with. While this is handy, there is no way to execute all the scripts in your project without opening each one and hitting 'Execute'. I have some projects with quite a lot of scripts, so this can get quite tedious. I decided to write a little add-in for SSMS to do this for me. To write an SSMS add-in is quite easy; just write a console application that accepts string arguments from SSMS and then add it as a external tool on the Tools->External Tools menu.

How the code works

When the console application is invoked, the current project is sent as an argument using the $(ProjectFileName) global variable. The application then makes a backup of the file before opening it. The project file is an XML format file with the extension 'ssmssqlproj'. The application looks for the first Connection object in the file and uses this connection to execute the scripts. It then loops through all the <FileNode> tags to get the SQL scripts included in the project and executes each one. The output is written to the SQL output window using Console.WriteLine().

Installing the add-in

To install the add-in:

  1. Click on the Tools->External Tools menu item
  2. Add a new tool using the Add button
  3. Give it a title such as 'Run Project Scripts'
  4. From the 'Command' edit box, browse to the RunScriptsInProject.exe file and select it
  5. In the 'Arguments' edit box, select $(ProjectFileName)
  6. In the 'Initial Directory' edit box, select $(ProjectDir)
  7. Check the 'Use Output Window' checkbox

Installing

For more information, look at this article where I got most of the idea from. It will also tell you how to add a toolbar button for your external tool.

Conclusion

This is a simple but useful add-in extension for SSMS that might make life a little easier for you. The source code can be modified to create your own add-ins for the IDE to maybe overcome some limitation. Any comments or suggestions are more than welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Netherlands Netherlands
I'm 27 years old and currently I'm working as a full time software developer/consultant in Amersfoort, Netherlands

Comments and Discussions

 
GeneralSelecting a database to run a project Pin
CeesCap15-Jul-09 1:42
CeesCap15-Jul-09 1:42 
GeneralNew version Pin
Michael Erasmus20-Nov-08 22:57
Michael Erasmus20-Nov-08 22:57 
GeneralTwo small enhancements: Pin
andr529-Jul-08 0:08
andr529-Jul-08 0:08 
GeneralBug fix for case sensitive "GO" and do while check Pin
Member 430465113-May-08 3:01
Member 430465113-May-08 3:01 
Generalerror with Add-In - No connectiound found... Pin
ka375130-Oct-07 9:11
ka375130-Oct-07 9:11 
AnswerRe: error with Add-In - No connectiound found... Pin
Michael Erasmus27-Nov-07 6:24
Michael Erasmus27-Nov-07 6:24 
GeneralRe: error with Add-In - No connectiound found... Pin
Wil Hutton23-May-08 6:40
Wil Hutton23-May-08 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.