Four SQL professionals gathered from the four corners of the world to share their SQL knowledge with each other and with their readers: Mickey Stuewe from California, USA, Chris Yates from Kentucky, USA, Julie Koesmarno from Canberra, Australia, and Jeffrey Verheul from Rotterdam, The Netherlands. They invite you to join them on their quest as they ask each other questions and seek out the answers in this collaborative blog series. This month we are cooperatively blogging about SQL Prompt by Red Gate.
The Next Mission
What makes up a SQL Prompt power user? Well, they use the SQL prompt commands like they were extensions of SSMS. They know the shortcuts, and they’ve memorized the key words to call up their favorite scripts. Let’s take a look what these short cuts are and how to integrate them seamlessly into your every day SQL Life.
Playing The Ebony Keys
A power user of any software is one who uses the hot keys in lieu of the menus. SQL prompt has a plethora of hot key combination.
Before I use <ctrl> + e to execute my code, I always use <ctrl> + k, <ctrl> + y. This hot key combo will structure your code based on the styles you have set in SQL prompt. This is my most heavily used hot key combo right after <ctrl> + e and <ctrl> + r (hiding the results window).
There is a new hot key combination that is coming out soon in version 6.4, and I’m very excited about it. It adds a semicolon at the end of each statement! I’ve been trying to add the habit of using semicolons for 2 years now, and it never sticks. Now I can use <ctrl> + b, <ctrl> + c and away I go.
Here is a table of the hot key combinations I use the most, plus the shortcut way I remember them.
|Hot Key Combination||Shortcut Hot Key Combo||What It Does|
|<ctrl> + b,
<ctrl> + w
|<ctrl> + bw||Expands ALL of the wild cards.|
|<ctrl> + b,
<ctrl> + q
|+ bq||Qualifies all the SQL Objects and fixes the capitalization as well.|
|in beta||<ctrl> + b,
<ctrl> + c
|<ctrl> + bc||Adds semicolons to the end of each statement. (Available soon in 6.4)|
|<ctrl> + k,
<ctrl> + y
|<ctrl> + ky||Standardizes the code based on the formatting styles that were set in SQL Prompt.|
As a power user, my key strokes will look like this when I use them all at once:
<ctrl> + bw, bq, bc, ky and finally <ctrl> + e to execute my code. Here is an example of how an ugly duckling turns into a beautiful swan by applying all of these hot keys at one time.
alter procedure [dbo].[salesteamassignment_rptugly](@group as varchar(1000), @startdate as date, @enddate as date)as set nocount on declare @uselessvar as varchar(max) select p.businessentityid ,firstname,lastname,s.name as store,[group] as TerritoryGroup,cr.name as CountryRegion,st.name as Territory,customercount,s.* from adventureworks2008r2.sales.salesperson as sp join adventureworks2008r2.person.person as p on sp.businessentityid = p.businessentityid join adventureworks2008r2.sales.salesterritory as st on sp.territoryid = st.territoryid join [adventureworks2008r2].[person].[countryregion] as cr on st.countryregioncode = cr.countryregioncode join adventureworks2008r2.sales.store as s on s.salespersonid = sp.businessentityid join dbo.fn_split(',', @group) as grps on st.[group] = convert(nvarchar(50), grps.separatedvalue) outer apply (select count(*) as customercount from adventureworks2008r2.sales.customer as c where s.businessentityid = c.storeid ) as cc
ALTER PROCEDURE dbo.SalesTeamAssignment_rptUGLY ( @group AS varchar(1000) ,@startdate AS date ,@enddate AS date ) AS SET NOCOUNT ON; DECLARE @uselessvar AS varchar(MAX); SELECT p.BusinessEntityID ,p.FirstName ,p.LastName ,s.Name AS store ,st.[Group] AS TerritoryGroup ,cr.Name AS CountryRegion ,st.Name AS Territory ,cc.customercount ,s.BusinessEntityID ,s.Name ,s.SalesPersonID ,s.Demographics ,s.rowguid ,s.ModifiedDate FROM AdventureWorks2008R2.Sales.SalesPerson AS sp JOIN AdventureWorks2008R2.Person.Person AS p ON sp.BusinessEntityID = p.BusinessEntityID JOIN AdventureWorks2008R2.Sales.SalesTerritory AS st ON sp.TerritoryID = st.TerritoryID JOIN AdventureWorks2008R2.Person.CountryRegion AS cr ON st.CountryRegionCode = cr.CountryRegionCode JOIN AdventureWorks2008R2.Sales.Store AS s ON s.SalesPersonID = sp.BusinessEntityID JOIN dbo.fn_Split(',', @group) AS grps ON st.[Group] = CONVERT(nvarchar(50), grps.SeparatedValue) OUTER APPLY ( SELECT COUNT(*) AS customercount FROM AdventureWorks2008R2.Sales.Customer AS c WHERE s.BusinessEntityID = c.StoreID ) AS cc;
But Wait, There’s More
Formatting your code is only half of the features in SQL Prompt. There are three other features that every power user should use. (Ok, the truth be told, there are more than 3, but my article shouldn’t be the length of a book.)
SQL Prompt offers the ability to save snippets of code. This is really useful for repetitive tasks. Here is a list of articles that I’ve written talking about specific snippets that I find handy.
Placeholders are one of the most helpful features that SQL Prompt provides for you to work with snippets. The placeholders are reserved words that are variables for specific pieces of information or represent actions that can be applied to your snippet. They are always completely capitalized and are surrounded by dollar signs ($). You can find the current list for SQL Prompt 6 here.
One of the new placeholders is prefect for the power user. It allows you to insert a snippet and have part of it highlighted. Here is an example of how I like to use it. This particular snippen, which I named “st1″, will add SELECT TOP 100 * FROM in front of the table name that I”ve highlighted and copied to the copy buffer.
$SELECTIONSTART$SELECT TOP 100 * FROM $PASTE$$SELECTIONEND$
The $PASTE$ placeholder will be replaced with whatever is in my copy buffer. The $SELECTIONSTART$ and $SELECTIONEND$ placeholders will cause the code within the placeholders to be highlighted.
To use it, I follow these steps.
1. I highlight a table I want to use and hit <ctrl> + c to get the table into my copy buffer.
2. I type st1 + to get the snippet. I now have a highlighted SELECT statement ready to execute, so I hit + e to execute it.
3. Finally, I type <ctrl> + z twice to undo the changes I’ve made.
This allows me to quickly see some sample data from the table without ever leaving my current query window.
This is a hidden gem. This feature finds all the invalid objects in your database. This is a great feature for those of you who need to perform code reviews and want to make sure that any DDL changes have not broken any existing SQL Objects.
In this example I have a table with a trigger that uses the field ModifiedDTS. I deleted the field and ran the command. It listed all the SQL Objects that are now broken due to the deleted field.
This feature does not have a Hot Key, but it is definitely worthy of a power user.
This is a great “clean up” feature, especially when you are working with stored procedures with 200 + lines that you have inherited. You know the ones, they are filled with old code and have never been cleaned up. Well, this feature is perfect for it, because it finds all the unused variables and parameters. Simply use the hot key combination + b, + f. SQL Prompt, will then underline in green all the unused variables and parameters for you to evaluate for usefulness.
I encourage you to spend 10 minutes going through the SQL Prompt menu to find out what other goodies exist for you to leverage. There are also new features coming soon in version 6.4. Here is a great reference to all the release notes for each version, plus the release notes for the current beta version.
Don’t Stop Yet
If you want to read more about how to work with SQL Prompt, then check out these blog posts:
- Chris Yates: SQL Prompt – The Power Within
- Julie Koesmarno: Clean And Tidy SQL With SQL Prompt
- Jeffrey Verheul: Become a T-SQL Hero with SQL Prompt
To follow our quest for SQL knowledge through this collaborative project, follow the #SQLCoOp tag on Twitter.
See you next month!!
Original Post: On a SQL Collaboration Quest