Using a SQL Prompt snippet with template parametersPublished 20 April 2012 12:00 pm
As part of my product management role I regularly attend trade shows and man the Red Gate booth in the vendor exhibition hall. Amongst other things this involves giving product demos to customers. Our latest demo involves SQL Source Control and SQL Test in a continuous integration environment.
In order to demonstrate quite how easy it is to set up our tools from scratch we start the demo by creating an entirely new database to link to source control, using an individual database name for each conference attendee. In SQL Server Management Studio this can be done either by selecting New Database from the Object Explorer or by executing “CREATE DATABASE DemoDB_John” in a query window.
We recently extended the demo to include SQL Test. This uses an open source SQL Server unit testing framework called tSQLt (www.tsqlt.org), which has a CLR object that requires EXTERNAL_ACCESS to be set as follows:
This isn’t hard to do, but if you’re giving demo after demo, this two-step process soon becomes tedious.
This is where SQL Prompt snippets come into their own.
I can create a snippet named create_demo_db for this following:
CREATE DATABASE DemoDB_John GO USE DemoDB_John GO ALTER DATABASE DemoDB_John SET TRUSTWORTHY ON
Now I just have to type the first few characters of the snippet name, select the snippet from SQL Prompt’s candidate list, and execute the code.
The problem is that this can only work once due to the hard-coded database name. Luckily I can leverage a nice feature in SQL Server Management Studio called Template Parameters.
If I modify my snippet to be:
CREATE DATABASE GO USE GO ALTER DATABASE SET TRUSTWORTHY ON
Once I’ve invoked the snippet, I can press Ctrl-Shift-M, which calls up the Specify Values for Template Parameters dialog, where I can type in my database name just once.
Now you can click OK and run the query. Easy.
Ideally I’d like for SQL Prompt to auto-invoke the Template Parameter dialog for all snippets where it detects the angled bracket syntax, but typing in the keyboard shortcut is a small price to pay for the time savings.
On April 24th (Europe) and May 1st (North America), Red Gate is hosting a free educational webinar covering existing and new features in SQL Prompt, including the new and exciting “experimental features”. Sign up here: http://www.red-gate.com/products/sql-development/sql-prompt/webinarstest