Templates in SSMS work really well; if you select to create a new stored procedure, you’ll get a skeleton procedure, and then placeholders, which you can populate by pressing Ctrl-Shift-M
But how can we replicate this same behaviour in a VS DB project?
Out of the box, creating a stored procedure will give you this:
The key to changing the above template is to create your own template. The templates in VS are stored wherever you tell them to be:
In my case:
C:\Users\Paul\Documents\Visual Studio 14\Templates\ItemTemplates
So, how to create a custom template for, say, a stored procedure? First, create your template. Typically, this will be from an existing VS template:
Now, make your changes:
And then, export the file:
File -> Export Template
You’ll then be asked which item in your project to export:
It treats DB templates like any other, so the next question relates to dependencies. Finally, it asks what you would like to call your template:
If you select to automatically import into VS, then after restarting VS, you should see your new template:
Using Common Templates with Source Control
You obviously can mail this around to the team, and they can all import it. However, another option is that you can source control it.
If you have a look at the directory above, you’ll see that it has created a file there:
Like any other file, you can simply add this to source control. If you do, then you can change the template directories described above to point at the source control folder. That way, everyone on the team can point to up to date source controlled templates.
Everything in this article applies equally well to other files – it’s just that it is very well documented for this, but not so well for SQL DB projects.