Using SQL Prompt to Accelerate the Top 10 Most Common Database Scripts

Comments 0

Share to social media

My Top 10 Most Common Database Scripts article described the top 10 T-SQL commands, as voted for by the SQLServerCentral community, which as a DBA you will find yourself tapping into your keyboard, day in day out. You’ll use some of these commands so often that they’ll virtually write themselves on your screen, but if you’re a keyboard dyslexic and also lazy, like me, then familiarity quickly breeds contempt for repetitive typing, and you will soon be looking for a way to run these commands with one or two keystrokes instead of twenty.

In this article, I’ll show how to use Redgate SQL Prompt to make that happen. I’m going to create snippets for each of the Top 10 T-SQL commands to see how I can make these commands much easier and faster to run on your systems.

I’ve exported my snippets (see the Download link, above) so that you don’t have to even retype what I’ve done.

#1 sp_who2 / sys.dm_exec_requests / sp_whoisactive

The first script for the SQL Prompt Snippet treatment is the one that interrogates a few Dynamic Management Objects and shows you what’s currently running on the system, the query text and the execution plan. Most of the code trips off your fingers after a while, though the section that parses the statements remains a little trickier. We can take away this pain by creating a snippet and giving it the command rn, for ‘right now‘, as shown in Figure 1.

2304-1-4b5acaad-f1a7-4c0b-b1d5-168dcf87c

Figure 1

Next time you need to run it, simply type rn, select the snippet and SQL Prompt will present the entire query for you, so you can see in a flash what is currently running on your server.

#2 STATISTICS IO/TIME

Typing STATISTICS IO doesn’t take much time, you might think, so surely any time saving is going to marginal? In fact, setting both commands ON, and then OFF again, gets dull pretty quickly, plus SQL Prompt has some built-in functions, called placeholders, that can help out even further.

Figure 2 shows the code for my new snippet, ssit

2304-1-1b58efdc-8bd8-4d80-9129-2a9c1bc5d

Figure 2

$SELECTEDTEXT$ and $CURSOR$ are placeholders. The former allows you to grab the currently selected text within the query window and use it with your snippet, and the latter is a marker for the cursor position, when the snippet completes, in case you want to type some additional code before you run your query.

To see this in action, simple highlight some code and look out for the little SQL Prompt icon to the left (introduced in SQL Prompt 7).

2304-1-c68ecaf7-8a72-47f3-8140-0f57a07be

Figure 3

Hit the control button once and let go to open the dropdown for this icon and you’ll see a list of commands and functions that you can execute against the selected code. Type in ssit to select the new snippet and hit enter to apply it. That’s just six key strokes to collect execution IO and timing statistics for any highlighted piece of code compared to about 32 with manual typing.

#3 BACKUP DATABASE

For the BACKUP DATABASE command, I’m going to create a snippet that allows me very quickly to create and run a database full backup that uses the WITHCOPY_ONLY clause.

Figure 4 shows the code for my bu snippet. You’ll just need to adjust the file locations as appropriate for your system.

2304-1-3c9bf77a-5f5c-46b2-ad5a-a02457795

Figure 4

This time, instead of using one of the built-in placeholders, I’ve created my own, called $DatabaseName$. This allows me to do something fun. I’ll type bu in a query window and hit space to fill in the code behind the snippet (I adjusted the default behavior so that the spacebar activates SQL Prompt on my machine).

Now for the fun part: SQL Prompt automatically highlights the placeholder, and presents a list of available databases on the SQL Server 2014 instance to this session is connected.

2304-1-20b8698a-6bd1-4b47-9c05-0e7859413

Figure 5

All I have to do is type a, and the AdventureWorks2014 database will be highlighted. I hit space again, it gets selected. That’s not all. Look what happens to the rest of the code…

2304-1-480a510c-2b84-4b04-be87-b9f6e0fe8

Figure 6

That’s right. Each of the placeholder locations has been replaced with the database name that I selected. I’ve got my backups running faster than ever and I don’t have to retype the name or copy and paste to get things right.

#4 sp_help

SQL Prompt already provides a built-in a snippet for running sp_help. Simply type sph and it will do the rest. I even edited my sph snippet so that it puts a semi-colon as a statement terminator.

#5 DBCC SQLPERF

Nothing special needed for this one. I created a new snippet for running DBCC SQLPERF(LOGSPACE). However, for really short commands like this, it’s almost faster to skip the snippet and just type them out. Using SQL Prompt’s auto-complete, I can type out the command in 9 keystrokes, so I’m only saving 6 keystrokes. Since we’re talking either 3 or 9 keystrokes out of a total of 24, it’s still a win.

2304-1-8f7ecae0-6c45-42f3-af5a-a9a7ca7c0

Figure 7

#6 sys.dm_exec_query_stats

There’s a ton of typing to be done to put together a good query against then sys.dm_exec_query_stats DMV, in order to return aggregated query statistics plus the SQL text and execution plan for previously executed queries that have plans in the plan cache. As a nice partner for the previous rn snippet for sys.dm_exec_requests, this one’s called nrn for ‘Not Right Now‘.

2304-1-12ef0563-05ae-43ec-9c42-b9fb50383

Figure 8

The only little addition here is the $CURSOR$ placeholder that upon running the snippet leaves the cursor in the right location to just keep typing, and add the appropriate WHERE clause for the query.

#7 RESTORE DATABASE

There are so many different options for running the RESTORE that it’s not easy to come up with a general-purpose snippet. Here, I’ll assume you simply want to restore a database from the full backup that results from #3, replacing the existing database. I’ve simplified the RESTORE options here, and you would need to adjust these on your systems depending on requirements. Here, I assume this is a simple restore of a full database backup for your development environment, and possibly from a secondary database. In such cases, Figure 9 shows a very simple snippet you could use.

2304-1-e110738e-4db5-4f23-be3d-56aded9d6

Figure 9

I’ve reused the custom $DatabaseName$ placeholder so that I can quickly supply the right name and have it used in all the appropriate places. I’ve also made sure that I have my RECOVERY script ready to go as well. This does save tons of typing.

#8 RESTORE FILELISTONLY

Again, RESTORE FILELISTONLY is fairly simple to type in, but I can still save myself some time.

2304-1-28c32382-fee5-4360-81df-17b8d37af

Figure 10

I don’t know about you, but I seldom type full path statements, especially the UNC path. Most of the time I’ll navigate to the folder so that I’m sure where it is and then I’ll copy the path from the Explorer window.

I take advantage of that with this rf snippet. The built-in placeholder, $PASTE$, will capture the last item copied to your clipboard, which in this case will be the full path and file name and the relevant backup.

#9 sp_spaceused

sp_spaceused is an excellent candidate for a snippet with some custom placeholders. Figure 11 shows how I’ve set this one up.

2304-1-12a45a89-da49-4b2f-8594-665435572

Figure 11

Note the additional wrinkle in Figure 11; I’ve taken advantage of the Default Value setting and entered a default of true for the @UpdateUsage argument, meaning that generally this command will rescan the system views so that they report up-to-date size and space information. However, it’s still easy to override and set it to false in cases where it’s not necessary.

2304-1-15004781-7a66-40a8-b31b-56dbdbb1b

Figure 12

I didn’t really have to use a placeholder for the @objname entry. Instead, I could have just ensured that the cursor dropped there between the quotes and left it to you to start typing. However, I like putting the parameters in order, so I have you set the @objname and then @updateusage. It’s a choice.

Speaking of which, you’ll note that when you start creating placeholders, the order you create them in is the default order in which they will be applied. However, you can change the order by using the arrows in the SQL Prompt Snippet Manager GUI to move them up or down.

#10 DBCC SHOW_STATISTICS

Finally, we have DBCC SHOW_STATISTICS and the snippet is very straight forward.

2304-1-39d7e2fc-4a63-4c8b-84c6-5401e7aa6

Figure 13

Nothing special this time; I just set it up the way it made sense, with custom placeholders for the table name and the name of the set of statistics (i.e. the column or index name).

Conclusion

I’ve been using SQL Prompt for a very long time now and it just keeps getting better, especially with all the new functionality around placeholders. This article demonstrated how to turn our ‘Top 10 Common T-SQL Commands’ into snippets that makes those commands much easier to implement within your systems.

SQL Prompt is available as a free 28-day trial, download it today and give it a try.

Article tags

Load comments

About the author

Grant Fritchey

See Profile

Grant Fritchey is a Data Platform MVP with over 30 years' experience in IT, including time spent in support and development. He has worked with SQL Server since 6.0 back in 1995. He has also developed in VB, VB.NET, C#, and Java. Grant has written books for Apress and Simple-Talk. Grant presents at conferences and user groups, large and small, all over the world. Grant volunteers for PASS and is on the Board of Directors as the Immediate Past President. He joined Redgate Software as a product advocate January 2011.

Grant Fritchey's contributions