TLDR: What I am ultimately looking for is to be able to highlight some text in the SQL Query Editor (which would be a partial object name), then press a keyboard shortcut (for example CTRL+5
) which would search the current database for objects based on the matching the highlighted text. Then locate the object in the results, copy it (or the aliased version), press CTRL+R
to close the grid and then paste. Then continue on with coding (and then probably do it again a minute later).
When coding / writing a query in SSMS using the SQL Query Editor, I constantly finding myself searching the database for objects to use in my current query. I use SSMS Boost, so my methodology is as follows.
- Open a new window (CTRL+N)
Use SSMS AutoReplace to paste the following code. (The cursor is placed where the
#
is located.)DECLARE @t nvarchar(100) SET @t = N'%#%' SELECT o.Type, o.Type_Desc, '['+SCHEMA_NAME(o.schema_id)+'].['+o.name+']' AS ObjectName, CASE WHEN Type IN (N'U') AND o.name LIKE N'%[_]%' THEN o.name + N' AS ' + STUFF(o.name, 1, CHARINDEX(N'_', o.name), N'') END AS Alias FROM sys.objects AS o WHERE o.name like @t AND RIGHT(REPLACE(o.name, N'sel', N''), 3) NOT IN (N'upd', N'del', N'ins', N'bdc') --AND Type IN (N'U') ORDER BY o.name
Type or paste the text
- Press
F5
to execute the query - Copy the object name from the results to use in the query
- Close the window with
CTRL+F4
- Paste the table (and alias) in the results
I've gotten pretty quick at doing this, but I still feel like there is a fair amount of time wasted.
In order to attempt to solve this problem and increase functionality with SSMS Boost, I have tried to use the Macro Functionality
- Copy highlighted text - Edit.Copy, (Global::Ctrl+C,Global::Ctrl+Ins)
- Open a new window - File.NewQuery, (Global::Ctrl+N)
- run the autoreplacement - ??? Or use another method of pasting the above SQL
- Paste the text - Edit.Paste, (Global::Ctrl+V,Global::Shift+Ins)
Since there does not appear to be the ability to add parameters / free text into the macros, I get stuck and cannot figure out what else to do. I tried posting on the SSMS Boost Forums, but I did not get much of a response.
I also tried to incorporate the native SSMS keyboard shortcuts without any success.
The following 2 queries work well with the SSMS shortcuts when an object (view or table) is valid. It also appears that the shortcuts work because the functionality allows for the highlighted text to be added to the end of the shortcut code / SQL.
SELECT COUNT(*) AS [Count] FROM --to get a quick count of the number of records in a table
SELECT TOP 100 * FROM --to get a glimpse of the data in the highlighted table or view
Finally, I looked into using SSMS Templates (which seems like pretty darn good functionality), but I could not figure out how to use them on the fly with a keyboard shortcut.