Tuesday 7 February 2012

SQL Server Management Studio keyboard shortcuts

SQL Server Management Studio keyboard shortcuts 

 

Problem

As responsibilities are growing every day, a DBA or developer needs to improve his/her productivity. One way to do this is to use as many shortcuts as possible instead of using your mouse and the menus. In this tip we take a look at common tasks you may perform when using SSMS and the associated shortcut keys.

Solution

We often overlook the shortcut keys which SSMS (SQL Server Management Studio) provides for increasing our productivity as a DBA or developer. In this tip series I will talk about some of these shortcut keys to help you use SSMS for more proficiently.

Launching SSMS

When launching SSMS you probably go through START-> All Programs -> SQL Server 20XX and click on SQL Server Management Studio, but this is not the only way.
Another options is to click on START -> Run or press Windows + R, type ssms and click OK (or hit ENTER) which will launch SSMS.
You can also specify different parameters or switches
  • The -E switch will let you connect to the local instance using Windows authentication.
  • The -U switch is used to specify a user and -P to specify the password
  • If you want SSMS to connect to a specific database you can use the -d switch
  • If you want a script file to be opened in SSMS you can specify the location and name of the file. This will just open the file in SSMS and will not execute the code. If you need to execute a script file you can use the SQLCMD utility.
  • To close SSMS you can use ALT+F4.
use run to launc ssms
As I said before, you can simply open the SSMS or you can specify the -E switch to open SSMS and connect using Windows authentication. If the current user does not have sufficient permissions obviously it will fail.
specify the e- switch to open ssms
When we open SSMS a splash screen appears while loading SSMS in the memory. You can specify -nosplash switch which opens SSMS without the splash screen.
open ssms without the splash screen
If you are scratching your head trying to remember all these switches, you don't need to because you can use -? which gives you the different command options as shown below. You can find more information about this here.
use -? which will give you all the command options
list of ssms switchs

Changing Databases

Once you are in a Query Window in SSMS you can use CTRL+U to change the database. When you press this combination, the database combo-box will be selected as shown below. You can then use the UPDOWN arrow keys to change between databases (or type a character to jump to databases starting with that character) select your database and hit ENTER to return back to the Query Window. and
in a query window in ssms you can change the database

Changing Code Case (Upper or Lower)

When you are writing code you may not bother with using upper or lower case to make your code easier to read. To fix this later, you can select the specific text and hit CTRL+SHIFT+U to make it upper case or use CTRL+SHIFT+L to make it lower case as shown below.
you can change code case after you are done writing it
changing code case in ssms

Commenting Out Code

When writing code sometimes you need to comment out lines of code. You can select specific lines and hit CTRL+K followed by CTRL+C to comment it out and CTRL+K followed by CTRL+U to uncomment it out as shown below.
you can comment out lines code
using commenting out code shortcuts in ssms

Indenting Code

As a coding best practice you should to indent your code for better readability. To increase the indent, select the lines of code (to be indented) and hit TAB as many times as you want to increase the indent likewise to decrease the indent again select those lines of code and hit SHIFT+TAB.
indenting code

Bookmarking Code

When you have hundreds of lines of code, it becomes difficult to navigate. In this case you can bookmark lines to which you would like to return to. Hit CTRL+K followed by CTRL+K again to toggle the bookmark on the line. When you bookmark a line a tiny light blue colored square appears on the left side of the query window to indicate that the line has been bookmarked as you can see in the image below.
You can press CTRL+K followed by CTRL+N to move to the next bookmarked line from the current cursor location likewise to move back to the last bookmarked line you can hit CTRL+K followed CTRL+P from the current cursor location.
To clear all bookmarks from the current window you can hit CTRL+K followed by CTRL+L.
bookmark lines of code you would like to reurn to later
There might be several bookmarks you have placed in your query window and to manage these easily SSMS provides a Bookmarks window. To launch this window simply hit CTRL+K followed by CTRL+W and you can manage almost every aspect of bookmarking from this window as shown below including renaming the bookmarks.
organize bookmarks in the ssms bookmark window

Search and Find / Replace Text

Sometimes you need to find specific keywords or replace some specific keyword with another keyword. To launch the Quick Find dialog box press CTRL+F or CTRL+H for the Quick Replace dialog box. You can even fine tune your search with other options available in the dialog box or you can bookmark all the lines which contain your search string. To close the dialog box press ESC. You can also press F3 to find the next keyword match.
using the quick find dialog box

Goto Line Number

If you know the line number you want to go to you can use CTRL+G to open the Go To Line dialog box and type the line number and press OK (or hit ENTER) to go to that particular line number.
go to line dialog box

Opening Query Windows and Switching Tabs

Next to open a new query window you can hit CTRL+N or to open a existing script file hit CTRL+O. You can hit CTRL+TAB to switch between open query windows.

More Query Window Shortcuts

Please note apart from the above mentioned shortcut keys these are some standard shortcut keys which work in SSMS as well.
  • CTRL+A to select all the text in the current query window
  • CTRL+C to copy selected text
  • CTRL+V to paste text
  • CTRL+X to cut selected text
  • DEL to delete text
  • CTRL+P to launch Print dialog box
  • CTRL+HOME to go to the beginning of the query window
  • CTRL+END to go to the end of the query window
  • CTRL+SHIFT+HOME to select all text from the current location to the beginning of the query window
  • CTRL+SHIFT+END to select all the text from the current location to the end of the query window
  • ALT+F4 to close SSMS

No comments :