In an earlier post, I showed how to create a DROP DATABASE template in SQL Server Management Studio (SSMS). At the time, I mentioned that a template wasn’t the best option because a command like this is normally inserted into a script; it’s not the whole script.
That’s where snippets shine. Let’s create a snippet for it.
First let’s open Code Snippets Manager (Tools > Code Snippets Manager):
You’ll see the existing snippet folders. I’ve clicked Add, then created a new folder called GL_Snippets.
Next I’ve created a file called DropDatabase.snippet on my desktop folder:
I then opened that file using SSMS:
Note that SSMS has a perfectly good XML editor. I’ve then used it to create the snippet file and saved it:
Back in Code Snippets Manager, I’ve clicked Import:
I located the file to import, deselected the My Code Snippets folder, and selected the GL_Snippets folder, then clicked Finish:
Now let’s try the new snippet. I opened a new query window, and right-clicked in the empty space. We can then see the option for Insert Snippet (note we could have used Ctrl+K, Ctrl+X):
Then select our new DropDatabase snippet:
The snippet will then appear, with the literal parameter highlighted, ready for replacement:
I typed SomeDatabase and then clicked elsewhere, and it’s all magically updated:
I hope you find this really useful for any code that you find yourself typing all the time.